Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
680 views
in Technique[技术] by (71.8m points)

maven - Build multiple artifacts with different classifiers at once

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.

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This can be done without profiles if you specify multiple plugin executions and resource filtering.

Create a properties file for each version in ${basedir}/src/main/filters (e.g. prod.properties, dev.properties) holding appropriate values for each environment.

Turn on filtering for your resources:

<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>

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>

Running mvn clean install should produce the properly filtered resources in artifacts with dev and prod classifiers like you want.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...