【问题标题】:Different MANIFEST.MF for default jar-file and tests.jar默认 jar 文件和 tests.jar 的不同 MANIFEST.MF
【发布时间】:2012-01-03 03:52:24
【问题描述】:

我正在尝试为 jar-packaged 工件和 test-jar-packaged 创建不同的 MANIFEST.MF 文件。 maven-jar-plugin 用于在MANIFEST.MF 中添加其他内容 - 到目前为止效果很好。但是,如果我想为 testproject 的 MANIFEST.MF 选择不同的模板文件,Maven 只对两个工件使用第二个引用的模板......

如何让 Maven 使用 PROD-MANIFEST.MF-template 进行正常的 jar 打包,使用 TEST-MANIFEST.MF-template 进行 test-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>

【问题讨论】:

    标签: java maven package manifest.mf maven-jar-plugin


    【解决方案1】:

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

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

    然后您使用配置文件调用 Maven

    mvn package -P PROD
    

    希望对您有所帮助。

    【讨论】:

    • 只有调用mvn 两次才有效。 mvn package -P PROD mvn package -P TEST 如果您在第二次调用时插入clean,则第一个清单将不正确。
    • 当您调用 Maven 时,它会构建并写入构建目录。所以,是的,构建将覆盖早期构建的文件。那是正常的行为。事实上,你所有的 Maven 运行都应该包括对clean 的调用;比如mvn clean package -P PROD
    • 但如果我包含clean 并执行maven -PTEST clean install,默认的jarfile 将包含错误的MANIFEST.MF(因为它不是使用配置文件PROD 构建的)。因此,此建议并未为该问题提供有效的解决方案。反正坦克!
    【解决方案2】:

    试试这个:

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

    此配置对同一插件执行 2 次不同的执行,每个执行都有自己的存档配置。

    如果在你的层次结构中的某个地方有一个父 pom,它在执行之外配置了存档,如下所示:

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

    那么该配置将与您默认的配置合并。如果您不希望这种情况发生,请将 combine.self 属性添加到 &lt;archive&gt; 元素,如下所示:

    <archive combine.self="override">
    

    plugins section of the POM reference 中所述。

    【讨论】:

      猜你喜欢
      • 2015-03-25
      • 2014-03-12
      • 1970-01-01
      • 1970-01-01
      • 2012-10-24
      • 2013-11-14
      • 2011-04-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多