问题 一次构建具有不同分类器的多个工件


我希望我的maven项目能够同时生成三个具有不同分类器的工件。我知道我可以使用模块等生成它。这实际上是一个资源项目,我想为DEV,STAGE和PROD环境生成配置。

我想拥有的就是奔跑 mvn:install 曾经有过 my.group:resources:1.0:devmy.group:resources:1.0:stage 和 my.group:resources:1.0:prod 在我的回购。


12318
2017-09-07 14:42


起源

不是骗局而是骗局 非常 类似的情况: stackoverflow.com/questions/3866784/... - chrisinmtown


答案:


如果您指定多个插件执行,则可以在没有配置文件的情况下完成此操作 资源过滤

为每个版本创建一个属性文件 ${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工件。


12
2017-09-07 15:45



在包中包括 prod.properties 和 dev.properties,如果我想让两个jar文件都一样,我该怎么办呢? config.properties? - Christian Vielma
@ChristianVielma,您是在询问在您的罐子中包含config.properties还是使用config.properties作为两个罐子的过滤器? - user944849
我的意思是如果我有config-dev.properties和config-prod.properties,我该如何在每个生成的jar中只有一个config.properties。我能解释一下吗? - Christian Vielma
在我的回答中,prod.props和dev.props文件是过滤器,而不是配置。您可以将config.properties放在/ src / main / resources中,在过滤期间将填充其中的值,最后在最终工件中最终得到一个文件。如果这还不够,你应该问一个单独的问题来描述你想要做的更详细的事情。 - user944849
这不包括库。在我的情况下,它甚至没有包括我的应用程序的类。 - Christian Vielma


仅供参考 - 将版本号放在那里以确保您拥有支持自定义过滤器的版本。在maven 3中,我设置了这样的例子。没有版本它没有用。

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-resources-plugin</artifactId>
    <version>2.6</version>
    ...
</plugin>

3
2017-07-04 01:55