我希望我的maven项目能够同时生成三个具有不同分类器的工件。我知道我可以使用模块等生成它。这实际上是一个资源项目,我想为DEV,STAGE和PROD环境生成配置。
我想拥有的就是奔跑 mvn:install
曾经有过 my.group:resources:1.0:dev
, my.group:resources:1.0:stage
和 my.group:resources:1.0:prod
在我的回购。
我希望我的maven项目能够同时生成三个具有不同分类器的工件。我知道我可以使用模块等生成它。这实际上是一个资源项目,我想为DEV,STAGE和PROD环境生成配置。
我想拥有的就是奔跑 mvn:install
曾经有过 my.group:resources:1.0:dev
, my.group:resources:1.0:stage
和 my.group:resources:1.0:prod
在我的回购。
如果您指定多个插件执行,则可以在没有配置文件的情况下完成此操作 资源过滤。
为每个版本创建一个属性文件 ${basedir}/src/main/filters
(例如prod.properties,dev.properties)为每个环境保存适当的值。
打开您的资源过滤:
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
现在添加资源插件执行。请注意不同的过滤器文件和输出目录。
<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插件;注意分类器和输入目录:
<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
像你想要的分类器。
在示例中,我使用了执行ID default-resources
和 default-jar
对于开发版本。如果没有这个,你在构建时也会获得一个未分类的jar工件。
仅供参考 - 将版本号放在那里以确保您拥有支持自定义过滤器的版本。在maven 3中,我设置了这样的例子。没有版本它没有用。
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
...
</plugin>