我有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?
发布于 2018-09-19 15:13:15
您可以通过两种方式进行此配置:
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><activeProfile>jdk-8linux</activeProfile>mvn test -P jdk-8-linux2)通过Maven设置的Profile激活
打开%USER_HOME%/.m2目录中可用的Maven %USER_HOME%/.m2文件,其中%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><activeProfile>linux</activeProfile>mvn test -P jdk-8-linux参考资料:
发布于 2018-09-19 14:02:44
在运行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>在定义配置文件特定属性之后,使用它们就像使用任何其他属性一样。
https://stackoverflow.com/questions/52407617
复制相似问题