在Maven中使用不同的artifactid构建相同的项目(基于使用的JDK) [英] Building same project in Maven with different artifactid (based on JDK used)

查看:870
本文介绍了在Maven中使用不同的artifactid构建相同的项目(基于使用的JDK)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个场景,其中我的项目需要在不同的JDK中编译,并且根据所使用的JDK,生成的工件名称应该是不同的。例如,如果项目名称是MyProject并且我调用mvn install,那么它需要在JDK 1.4以及JDK 1.5中编译,最后我得到两个相同项目的jar(MyProjectJDK14-1.0和MyProjectJDK15-1.0)。有可能实现这个目标吗?

I have a scenario wherein my project needs to be compiled in different JDKs and the resulting artifact name should be different based on the JDK used. For example if the project name is MyProject and I call mvn install then it needs to be compiled in JDK 1.4 as well as JDK 1.5, and finally I get two jars of the same project (MyProjectJDK14-1.0 and MyProjectJDK15-1.0). Is is possible to achieve this?

推荐答案

Maven的做法是不要改变 finalName 的工件,但要使用分类器。例如:

The Maven way to do this is not to change the finalName of the artifact but to use a classifier. For example:

<project>
  ...
  <build>
    <plugins>
      <plugin>
        <artifactId>maven-jar-plugin</artifactId>
        <configuration>
          <classifier>${envClassifier}</classifier>
        </configuration>
      </plugin>
    </plugins>
  </build>
  ...
  <profiles>
    <profile>
      <id>jdk16</id>
      <activation>
        <jdk>1.6</jdk>
      </activation>
      <properties>
        <envClassifier>jdk16</envClassifier>
      </properties>
    </profile>
    <profile>
      <id>jdk15</id>
      <activation>
        <jdk>1.5</jdk>
      </activation>
      <properties>
        <envClassifier>jdk15</envClassifier>
      </properties>
    </profile>
  </profiles>
</project>

JAR工件将命名为 $ {finalName} - $ {envClassifier} .jar 并使用以下语法作为依赖项包含在内:

The JAR artifact will be named ${finalName}-${envClassifier}.jar and included as a dependency using the following syntax:

<dependency>
  <groupId>com.mycompany</groupId>
  <artifactId>my-project</artifactId>
  <version>1.0</version>
  <classifier>jdk16</classifier>
</dependency>

你必须两次调用Maven构建才能生成两个罐子(一个体面的CI引擎可以做那个)。

You'll have to call the Maven build twice to produce both jars (a decent CI engine can do that).

这篇关于在Maven中使用不同的artifactid构建相同的项目(基于使用的JDK)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆