mavenjava:设置pom。xml属性
我有一个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
# 1 楼答案
您可以通过两种方式进行此配置:
1) 显式配置文件
打开项目目录中可用的Maven
pom.xml
文件:<activeProfile>jdk-8linux</activeProfile>
mvn test -P jdk-8-linux
2) 通过Maven设置激活配置文件
打开Maven
settings.xml
文件,该文件位于%USER_HOME%/.m2
目录中,其中%USER_HOME%
表示用户主目录。如果settings.xml
文件不存在,则创建一个新文件<activeProfile>linux</activeProfile>
mvn test -P jdk-8-linux
参考资料:
Maven - Build Profiles
Compiling Sources Using A Different JDK
# 2 楼答案
运行
mvn
命令时,可以将属性作为命令行参数传递,例如:mvn test"-DJAVA_1_8_HOME=<OS specific path>"
关于另一种解决方案,请看maven condition based on os family
对于配置文件:
定义特定于概要文件的属性后,请像使用任何其他属性一样使用它们