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

Pio*_*zda 13 maven-3 maven

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

我想拥有的是运行mvn:install一次并拥有my.group:resources:1.0:dev,my.group:resources:1.0:stagemy.group:resources:1.0:prod在我的回购中.

use*_*849 14

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

为每个版本${basedir}/src/main/filters(例如prod.properties,dev.properties)创建一个属性文件,为每个环境保存适当的值.

打开您的资源过滤:

<resources>
  <resource>
    <directory>src/main/resources</directory>
    <filtering>true</filtering>
  </resource>
</resources>
Run Code Online (Sandbox Code Playgroud)

现在添加资源插件执行.请注意不同的过滤器文件和输出目录.

<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>
Run Code Online (Sandbox Code Playgroud)

最后,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>
Run Code Online (Sandbox Code Playgroud)

运行mvn clean install应该在工件中生成正确过滤的资源,dev并使用prod您想要的分类器.

在这个例子中,我使用的执行标识default-resourcesdefault-jar用于开发的版本.如果没有这个,你在构建时也会得到一个未分类的jar工件.