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>"
其他解决方案,请查看maven condition based on os family
个人资料:
<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
参考:
我有 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>"
其他解决方案,请查看maven condition based on os family
个人资料:
<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