首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >设置pom.xml属性

设置pom.xml属性
EN

Stack Overflow用户
提问于 2018-09-19 13:56:48
回答 2查看 1.2K关注 0票数 0

我有Java Meven项目,在我的pom.xml中有这个property

代码语言:javascript
复制
<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,那么这个路径就不存在了,我想知道有什么解决方案可以解决这个问题。

更新

作为建议,我在这里添加以下简介:

代码语言:javascript
复制
<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?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-09-19 15:13:15

您可以通过两种方式进行此配置:

1)显式轮廓

打开项目目录中可用的Maven pom.xml文件:

代码语言:javascript
复制
<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设置的Profile激活

打开%USER_HOME%/.m2目录中可用的Maven %USER_HOME%/.m2文件,其中%USER_HOME%表示用户主目录。如果settings.xml文件不存在,那么创建一个新的文件。

代码语言:javascript
复制
<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

参考资料:

票数 1
EN

Stack Overflow用户

发布于 2018-09-19 14:02:44

在运行mvn命令时,可以将该属性作为命令行参数传递,例如:

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

关于另一种解决方案,请看一看maven condition based on os family

配置文件的

代码语言:javascript
复制
<project>
    <profiles>
         <profile>
             <properties>
                 // Define profile specific properties here
             </properties>
         </profile>
    </profiles>
</project>

在定义配置文件特定属性之后,使用它们就像使用任何其他属性一样。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52407617

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档