一次使用不同的分类器构建多个工件 [英] Build multiple artifacts with different classifiers at once
问题描述
W希望我的Maven项目一次生成具有不同分类器的三个工件.我知道我可以用模块等生成它.这实际上是一个资源项目,我想为DEV,STAGE和PROD环境生成配置.
W want my maven project to produce three artifacts with different classifiers at once. I know that I can produce it with modules etc. This is actually a resources project that I want to produce configuration for DEV, STAGE and PROD environment.
我要运行一次mvn:install
,并在回购中包含my.group:resources:1.0:dev
,my.group:resources:1.0:stage
和my.group:resources:1.0:prod
.
What I want to have is to run mvn:install
once and have my.group:resources:1.0:dev
, my.group:resources:1.0:stage
and my.group:resources:1.0:prod
in my repo.
推荐答案
This can be done without profiles if you specify multiple plugin executions and resource filtering.
为${basedir}/src/main/filters
中的每个版本创建一个属性文件(例如prod.properties,dev.properties),并为每个环境保存适当的值.
Create a properties file for each version in ${basedir}/src/main/filters
(e.g. prod.properties, dev.properties) holding appropriate values for each environment.
打开对资源的过滤:
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
现在添加资源插件执行.请注意不同的过滤器文件和输出目录.
Now add the resource plugin executions. Note the different filter file and output directory.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>default-resources</id>
<phase>process-resources</phase>
<goals>
<goal>resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.outputDirectory}/dev</outputDirectory>
<filters>
<filter>${basedir}/src/main/filters/dev.properties</filter>
</filters>
</configuration>
</execution>
<execution>
<id>prod</id>
<phase>process-resources</phase>
<goals>
<goal>resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.outputDirectory}/prod</outputDirectory>
<filters>
<filter>${basedir}/src/main/filters/prod.properties</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
最后,jar插件;笔记分类器和输入目录:
Finally, the jar plugin; note classifier and input directory:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<id>default-jar</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classifier>dev</classifier>
<classesDirectory>${project.build.outputDirectory}/dev</classesDirectory>
</configuration>
</execution>
<execution>
<id>jar-prod</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classifier>prod</classifier>
<classesDirectory>${project.build.outputDirectory}/prod</classesDirectory>
</configuration>
</execution>
</executions>
</plugin>
运行mvn clean install
应该使用所需的dev
和prod
分类器在工件中生成经过适当过滤的资源.
Running mvn clean install
should produce the properly filtered resources in artifacts with dev
and prod
classifiers like you want.
在示例中,对于开发版本,我使用了default-resources
和default-jar
的执行ID.如果没有此功能,则在构建时也会得到未分类的jar工件.
In the example, I used execution IDs of default-resources
and default-jar
for the dev versions. Without this you would also get an unclassified jar artifact when you build.
这篇关于一次使用不同的分类器构建多个工件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!