2

I'm trying to create different MANIFEST.MF files for the jar-packaged artifacts and the test-jar-packaged. The maven-jar-plugin being used to add additional stuff into the MANIFEST.MF - that works perfectly so far. But if I'd like to chose different template file for the MANIFEST.MF for the testproject, Maven only uses the second referenced template for both artifacts...

How can I get Maven to use the PROD-MANIFEST.MF-template for the normal jar-packaging and the TEST-MANIFEST.MF-template for test-jar-packaging?

<plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <executions>
            <execution>
                <id>test-manifest-mf</id>
                <phase>package</phase>
                <goals>
                    <goal>test-jar</goal>
                </goals>
            </execution>
        </executions>
        <configuration>
            <archive>
                <manifest>
                    <addClasspath>true</addClasspath>
                </manifest>
                <manifestFile>foo/TEST-MANIFEST.MF</manifestFile>
            </archive>
        </configuration>
    </plugin>

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <executions>
            <execution>
                <id>default-manifest-mf</id>
                <phase>package</phase>
                <goals>
                    <goal>jar</goal>
                </goals>
            </execution>
        </executions>
        <configuration>
            <archive>
                <manifest>
                    <addClasspath>true</addClasspath>
                </manifest>
                <manifestFile>foo/PROD-MANIFEST.MF</manifestFile>
            </archive>
        </configuration>
    </plugin>
Piotr Nowicki
  • 17,914
  • 8
  • 63
  • 82
polemoser
  • 59
  • 6

2 Answers2

1

Wrap each plugin configuration you have provided in a profile.

<profiles>
  <profile>
    <id>PROD</id>
    <build>
      <plugins>
        // your PROD plugin configuration
      </plugins>
    </build>
  </profile>
  <profile>
    <id>TEST</id>
    <build>
      <plugins>
        // your TEST plugin configuration
      </plugins>
    </build>
  </profile>
</profiles>

Then you invoke Maven with a profile

mvn package -P PROD

Hope that helps.

Sri Sankaran
  • 8,120
  • 4
  • 38
  • 47
  • It only works if you call `mvn` twice. `mvn package -P PROD` `mvn package -P TEST` If you insert a `clean` at the second call the first manifest won't be correct. – polemoser Nov 23 '11 at 08:56
  • When you invoke Maven it builds and writes to the build directory. So, yes a build will overwrite an earlier build's files. That is normal behavior. In fact all your Maven runs should include a call to `clean`; such as `mvn clean package -P PROD`. – Sri Sankaran Nov 23 '11 at 11:34
  • But if I include a `clean` and perform a `maven -PTEST clean install` the default jarfile will contain the wrong MANIFEST.MF (because it's not built with profile `PROD`). Therefore this suggestion does not provide a valid solution for the problem. Tanks anyway! – polemoser Nov 23 '11 at 11:49
1

Try this:

<plugins>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <executions>
      <execution>
        <id>test-manifest-mf</id>
        <phase>package</phase>
        <goals>
            <goal>test-jar</goal>
        </goals>
        <configuration>
          <archive>
            <manifest>
                <addClasspath>true</addClasspath>
            </manifest>
            <manifestFile>foo/TEST-MANIFEST.MF</manifestFile>
          </archive>
        </configuration>
      </execution>

      <execution>
        <id>default-manifest-mf</id>
        <phase>package</phase>
        <goals>
            <goal>jar</goal>
        </goals>
        <configuration>
          <archive>
            <manifest>
              <addClasspath>true</addClasspath>
            </manifest>
            <manifestFile>foo/PROD-MANIFEST.MF</manifestFile>
          </archive>
        </configuration>
      </execution>
    </executions>
  </plugin>
</plugins>

This configuration is performing 2 different executions of the same plugin, each of which has its own archive configuration.

If there is a parent pom somewhere in your hierarchy that has the archive configured outside of an execution, like this:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-jar-plugin</artifactId>
  <configuration>
    <archive>
       ... other archive config ...
    </archive>
  </configuration>
</plugin>

then that configuration will be merged with what you have by default. If you don't want that to happen, add the combine.self attribute to the <archive> element like so:

<archive combine.self="override">

as described in the plugins section of the POM reference.

user944849
  • 14,524
  • 2
  • 61
  • 83