Java 默认jar文件和tests.jar的MANIFEST.MF不同

Java 默认jar文件和tests.jar的MANIFEST.MF不同,java,maven,package,manifest.mf,maven-jar-plugin,Java,Maven,Package,Manifest.mf,Maven Jar Plugin,我试图为jar打包的工件和测试jar打包的工件创建不同的MANIFEST.MF文件。maven jar插件用于向MANIFEST.MF中添加额外的内容,目前为止,该插件运行良好。但是如果我想为testproject的MANIFEST.MF选择不同的模板文件,Maven只为这两个工件使用第二个引用的模板 如何让Maven使用PROD-MANIFEST.MF模板进行普通jar包装,使用TEST-MANIFEST.MF模板进行测试jar包装 <plugins> <plugin

我试图为jar打包的工件和测试jar打包的工件创建不同的
MANIFEST.MF
文件。
maven jar插件
用于向
MANIFEST.MF
中添加额外的内容,目前为止,该插件运行良好。但是如果我想为testproject的
MANIFEST.MF
选择不同的模板文件,Maven只为这两个工件使用第二个引用的模板

如何让Maven使用
PROD-MANIFEST.MF模板
进行普通jar包装,使用
TEST-MANIFEST.MF模板
进行测试jar包装

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

org.apache.maven.plugins
maven jar插件
测试舱单mf
包裹
试验罐
真的
foo/TEST-MANIFEST.MF
org.apache.maven.plugins
maven jar插件
默认清单mf
包裹
罐子
真的
foo/PROD-MANIFEST.MF

将您在配置文件中提供的每个插件配置包装起来

<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>
希望能有所帮助。

试试这个:

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

如。

中所述,仅当您调用
mvn
两次时,此功能才起作用
mvn package-P PROD
mvn package-P TEST
如果在第二次调用时插入
clean
,则第一个清单将不正确。当调用Maven时,它将生成并写入生成目录。因此,是的,生成将覆盖早期生成的文件。这是正常的行为。事实上,所有Maven运行都应该包括对
clean
的调用;例如
mvn clean package-P PROD
。但是如果我包括
clean
并执行
maven-PTEST clean安装
,默认的jar文件将包含错误的MANIFEST.MF(因为它不是用profile
PROD
构建的)。因此,这项建议并不能有效地解决这个问题。反正是坦克!
<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>
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-jar-plugin</artifactId>
  <configuration>
    <archive>
       ... other archive config ...
    </archive>
  </configuration>
</plugin>
<archive combine.self="override">