Java 使用不同的 artifactid 在 Maven 中构建相同的项目(基于使用的 JDK)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3092085/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Building same project in Maven with different artifactid (based on JDK used)
提问by Manoj
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?
我有一个场景,其中我的项目需要在不同的 JDK 中编译,并且生成的工件名称应该根据使用的 JDK 不同。例如,如果项目名称是 MyProject 并且我调用 mvn install 那么它需要在 JDK 1.4 和 JDK 1.5 中编译,最后我得到同一个项目的两个 jar(MyProjectJDK14-1.0 和 MyProjectJDK15-1.0)。有可能实现这一目标吗?
采纳答案by Pascal Thivent
The Maven way to do this is not to change the finalName
of the artifact but to use a classifier. For example:
执行此操作的 Maven 方法不是更改finalName
工件的 ,而是使用分类器。例如:
<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>
The JAR artifact will be named ${finalName}-${envClassifier}.jar
and included as a dependency using the following syntax:
JAR 工件将${finalName}-${envClassifier}.jar
使用以下语法命名并作为依赖项包含在内:
<dependency>
<groupId>com.mycompany</groupId>
<artifactId>my-project</artifactId>
<version>1.0</version>
<classifier>jdk16</classifier>
</dependency>
You'll have to call the Maven build twice to produce both jars (a decent CI engine can do that).
您必须调用 Maven 构建两次才能生成两个 jar(一个不错的 CI 引擎可以做到这一点)。
回答by Romain Linsolas
What you can do is to define two profiles, one per JDK used. Each profile will be activated regarding which JDK is used:
您可以做的是定义两个配置文件,每个使用的 JDK 一个。每个配置文件都将根据使用的 JDK 进行激活:
<profiles>
<profile>
<id>profile-for-jdk1.4</id>
<activation>
<activeByDefault>false</activeByDefault>
<jdk>1.4</jdk>
</activation>
<build>
<finalName>myBuild-jdk1.4</finalName>
</build>
</profile>
<profile>
<id>profile-for-jdk1.5</id>
<activation>
<activeByDefault>false</activeByDefault>
<jdk>1.5</jdk>
</activation>
<build>
<finalName>myBuild-jdk1.5</finalName>
</build>
</profile>
</profiles>
Then, in each profile, you define a specific <finalName>
, which will be used to name the generated JAR file.
然后,在每个配置文件中,您定义一个特定的<finalName>
,它将用于命名生成的 JAR 文件。
Thus, if you build your application using JDK 1.4, the generated JAR will be named myBuild-jdk1.4.jar
.
因此,如果您使用 JDK 1.4 构建应用程序,则生成的 JAR 将命名为myBuild-jdk1.4.jar
.
If your final package is built using an assembly, you can simply change the <build>
block inside profiles to configure the assembly plugin (for example to <finalName>
).
如果您的最终包是使用程序集构建的,您可以简单地更改<build>
配置文件内的块以配置程序集插件(例如到<finalName>
)。
Regarding your comment:Indeed, this procedure will need two separate builds on Maven, as you have to recompile the whole project when changing the JDK version. One of the Maven2 convention is that one project = one artifact. What you want is to have one project with two artifacts.
关于您的评论:确实,此过程将需要在 Maven 上进行两个单独的构建,因为在更改 JDK 版本时您必须重新编译整个项目。Maven2 约定之一是一个项目 = 一个工件。您想要的是拥有一个包含两个工件的项目。
Eventually, one solution is to use Hudsonto build your application, and especially the matrix featureof this tool, which allows you to run multiple builds with various parameters, in your case the JDK.
最终,一种解决方案是使用Hudson来构建您的应用程序,尤其是该工具的矩阵功能,它允许您使用各种参数运行多个构建,在您的情况下是 JDK。
回答by Abhinav Sarkar
Use Maven Profiles. Add this section inside the project
tag of your pom.xml
:
使用 Maven 配置文件。将此部分添加project
到您的标签中pom.xml
:
<profiles>
<profile>
<activation>
<jdk>1.4</jdk>
</activation>
<build>
<finalName>${project.artifactId}-${project.version}-JDK1.4</finalName>
</build>
</profile>
<profile>
<activation>
<jdk>1.5</jdk>
</activation>
<build>
<finalName>${project.artifactId}-${project.version}-JDK1.5</finalName>
</build>
</profile>
</profiles>
See thisto know more about profiles.
请参阅此以了解有关配置文件的更多信息。
回答by raymi
There is actually a way to produce more than one WAR with one build (I guess this works for JARs as well): you can use the assembly plugin with multiple executions for different descriptors.
实际上有一种方法可以通过一次构建生成多个 WAR(我想这也适用于 JAR):您可以使用程序集插件对不同的描述符进行多次执行。
回答by TimP
A similar problem is the different variants of the JDBC api used in different versions of the JDK.
一个类似的问题是不同版本的 JDK 中使用的 JDBC api 的不同变体。
I decided that these needed different arifactIds rather than classifiers.
我决定这些需要不同的 arifactIds 而不是分类器。
You can achieve this by setting a property in settings and then referencing this in the artifactId tag:
您可以通过在 settings 中设置一个属性然后在 artifactId 标签中引用它来实现这一点:
<project>
<modelVersion>4.0.0</modelVersion>
<artifactId>throwing-jdbc-${jdbc.version}</artifactId>
<name>Throwing JDBC</name>
<profiles>
<profile>
<id>jdbc3</id>
<activation>
<activeByDefault>false</activeByDefault>
<jdk>[1.3,1.4,1.5]</jdk>
</activation>
<properties>
<jdbc.version>3.0</jdbc.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.7</version>
<configuration>
<sources>
<source>src/jdbc3-variants/java</source>
</sources>
</configuration>
<executions>
<execution>
<goals>
<goal>add-source</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>jdbc4</id>
<activation>
<activeByDefault>false</activeByDefault>
<jdk>1.6</jdk>
</activation>
<properties>
<jdbc.version>4.0</jdbc.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<configuration>
<sources>
<source>src/jdbc4/java</source>
<source>src/jdbc4-variants/java</source>
</sources>
</configuration>
<executions>
<execution>
<goals>
<goal>add-source</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>jdbc41</id>
<activation>
<activeByDefault>false</activeByDefault>
<jdk>1.7</jdk>
</activation>
<properties>
<jdbc.version>4.1</jdbc.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<configuration>
<sources>
<source>src/jdbc4/java</source>
<source>src/jdbc4.1/java</source>
<source>src/jdbc4.1-variants/java</source>
</sources>
</configuration>
<executions>
<execution>
<goals>
<goal>add-source</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<sourceDirectory>src/main/java</sourceDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.2.1</version>
<executions>
<execution>
<phase>verify</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>