Build multiple artifacts with different classifiers at once

前端 未结 2 807
Happy的楠姐
Happy的楠姐 2020-12-05 19:28

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 th

相关标签:
2条回答
  • 2020-12-05 20:21

    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>
    
    0 讨论(0)
  • 2020-12-05 20:23

    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.

    0 讨论(0)
提交回复
热议问题