簡體   English   中英

Java:設置pom.xml屬性

[英]Java: set pom.xml property

我有Java Meven項目,在pom.xml我具有以下property

<properties>
    <suiteXmlFile>testing.xml</suiteXmlFile>
    <JAVA_1_8_HOME>C:\Program Files\Java\jdk1.8.0_181\bin\javac.exe</JAVA_1_8_HOME>
</properties>

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.8.0</version>
    <configuration>
         <fork>true</fork>
         <executable>${JAVA_1_8_HOME}</executable>
    </configuration>
</plugin>

所以我想我正在從Windows運行我的項目,我只需要輸入mvn test

如果我使用MACOS/Linux此路徑不存在,我想知道可以找到什么解決方案來解決此問題。

更新

作為建議,我在此添加此配置文件:

<profile>
            <id>platform-windows</id>
            <activation>
                <os>
                    <family>windows</family>
                </os>
            </activation>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-compiler-plugin</artifactId>
                        <version>3.8.0</version>
                        <configuration>
                            <fork>true</fork>
                            <executable>C:\Program Files\Java\jdk1.8.0_181\bin\javac.exe</executable>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>

        <profile>
            <id>mac</id>
            <activation>
                <os>
                    <family>mac</family>
                </os>
            </activation>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-compiler-plugin</artifactId>
                        <version>3.8.0</version>
                        <configuration>
                            <fork>true</fork>
                            <executable>/usr/bin/javac</executable>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>

現在我的代碼將如何運行此特定ID?

您可以在運行mvn命令時將該屬性作為命令行參數傳遞,例如:

mvn test"-DJAVA_1_8_HOME=<OS specific path>"

對於其他解決方案,請查看基於os系列的Maven條件

對於配置文件:

<project>
    <profiles>
         <profile>
             <properties>
                 // Define profile specific properties here
             </properties>
         </profile>
    </profiles>
</project>

定義配置文件特定的屬性后,請像使用其他任何屬性一樣使用它們。

您可以通過兩種方式進行此配置:


1)明確的個人資料

打開項目目錄中可用的Maven pom.xml文件:

<properties>
    <suiteXmlFile>testing.xml</suiteXmlFile>
    <JAVA_1_8_HOME>C:\Program Files\Java\jdk1.8.0_181\bin\javac.exe</JAVA_1_8_HOME>
    <JAVA_1_8_HOME_LINUX>/usr/java/jdk1.8.0_181/</JAVA_1_8_HOME_LINUX>
</properties>

<profiles>
    <!-- Windows Profile-->
    <profile>
        <id>jdk-8-windows</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
       <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.8.0</version>
                    <configuration>
                        <fork>true</fork>
                        <executable>${JAVA_1_8_HOME}</executable>
                        <source>1.8</source>
                        <target>1.8</target>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>

   <!-- Mac/Linux Profile-->
    <profile>
        <id>jdk-8-linux</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.8.0</version>
                    <configuration>
                        <fork>true</fork>
                        <executable>${JAVA_1_8_HOME_LINUX}</executable>
                        <source>1.8</source>
                        <target>1.8</target>
                    </configuration>
                </plugin>
            </plugins>
        </build>
   </profile>

</profiles>
  • 默認活動配置文件定義為:jdk-8-windows
  • 如果主配置文件是Mac / Linux,請使用: <activeProfile>jdk-8linux</activeProfile>
  • 要執行Mac / Linux配置文件,請使用: mvn test -P jdk-8-linux

2)通過Maven設置激活配置文件

打開%USER_HOME%/.m2目錄中的Maven settings.xml文件,其中%USER_HOME%代表用戶主目錄。 如果settings.xml文件不存在,請創建一個新文件。

<settings>
  [...]
  <profiles>
    [...]

    <!-- Windows Profile-->
    <profile>
      <id>jdk-8-windows</id>
        <properties>
          <JAVA_1_8_HOME>C:\Program Files\Java\j2sdk1.4.2_09</JAVA_1_8_HOME>
        </properties>
    </profile>

    <!-- Mac/Linux Profile-->
    <profile>
      <id>jdk-8-linux</id>
        <properties>
          <JAVA_1_8_HOME_LINUX>/usr/bin/javac</JAVA_1_8_HOME_LINUX>
        </properties>
    </profile>

  </profiles>
  [...]
  <activeProfiles>
    <activeProfile>windows</activeProfile>
  </activeProfiles>
</settings>
  • 默認活動配置文件定義為:jdk-8-windows
  • 如果主配置文件是Mac / Linux,請使用: <activeProfile>linux</activeProfile>
  • 要執行Mac / Linux配置文件,請使用: mvn test -P jdk-8-linux

參考:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM