Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Build multiple artifacts with different classifiers at once

Tags:

maven

maven-3

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.

like image 477
Piotr Gwiazda Avatar asked Sep 07 '12 14:09

Piotr Gwiazda


2 Answers

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.

like image 143
user944849 Avatar answered Oct 30 '22 18:10

user944849


Just an FYI - put the version number in there to make sure you have the version supporting custom filters. In maven 3 I set mine up like this for example. Without version it didn't work.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-resources-plugin</artifactId>
    <version>2.6</version>
    ...
</plugin>
like image 42
Matt Byrne Avatar answered Oct 30 '22 17:10

Matt Byrne