【问题标题】:Unable to add classpath in MANIFEST.MF in maven2无法在 Maven2 的 MANIFEST.MF 中添加类路径
【发布时间】:2010-08-10 06:49:58
【问题描述】:

我正在尝试使用以下代码在 maven2 的 MANIFEST.MF 中添加类路径,但无法添加。

<build>
  <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-compiler-plugin</artifactId>
          <configuration>
            <source>1.5</source>
            <target>1.5</target>
            <manifest>
              <addClasspath>true</addClasspath>
              <useUniqueVersions>false</useUniqueVersions>
            </manifest>
          </configuration>
        </plugin>
      </plugins>
    </pluginManagement>
   <finalName>iHubServiceImpl</finalName>
</build>

你能帮帮我吗?


更新:下面更新了 pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.adp.ihub</groupId>
    <artifactId>PreFinal</artifactId>
    <version>1</version>
  </parent>
  <groupId>com.adp.ihub</groupId>
  <artifactId>iHubCommon</artifactId>
  <version>1</version>
  <packaging>jar</packaging>
  <name>iHubCommon</name>
  <url>http://maven.apache.org</url>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  <build>
    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-jar-plugin</artifactId>
          <version>2.3.1</version>
          <configuration>
            <archive>
              <manifest>
                <addClasspath>true</addClasspath>
                <classpathLayoutType>simple</classpathLayoutType>
              </manifest>
              <manifestEntries>
                <mode>development</mode>
                <url>${pom.url}</url>
                <key>value</key>
              </manifestEntries>
            </archive>
          </configuration>
        </plugin>
      </plugins>
    </pluginManagement>
    <plugins>
      <!-- Use the jar plugin for plugin management configuration to take effect -->    
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
      </plugin>
    </plugins>
    <sourceDirectory>${basedir}/src</sourceDirectory>
    <resources>
      <resource>
        <directory>${basedir}/src</directory>
        <excludes>
          <exclude>**/*.java</exclude>
          <exclude>**/pom*</exclude>
        </excludes>
      </resource>
    </resources>
    <finalName>iHubCommon</finalName>
  </build>
  <dependencies>
    <dependency>
      <groupId>com.adp.ihub</groupId>
      <artifactId>BizLogic3</artifactId>
      <version>1</version>
      <scope>system</scope>
      <systemPath>${PWD}/iHUBCM/Environment/lib/BizLogic3.jar</systemPath>
    </dependency>
    <dependency>
      <groupId>com.adp.ihub</groupId>
      <artifactId>EncryptionAPI-jdk15-0.4</artifactId>
      <version>1</version>
      <scope>system</scope>
      <systemPath>${PWD}/iHUBCM/Environment/lib/EncryptionAPI-jdk15-0.4.jar</systemPath>
    </dependency>
    <dependency>
      <groupId>com.adp.ihub</groupId>
      <artifactId>adpbod-1.0</artifactId>
      <version>1</version>
      <scope>system</scope>
      <systemPath>${PWD}/iHUBCM/Environment/lib/adpbod-1.0.jar</systemPath>
    </dependency>
  </dependencies>
</project>

但我仍然没有得到 manifest.mf 中的条目。怎么了?

【问题讨论】:

    标签: maven-2 manifest.mf


    【解决方案1】:

    对于Add a Class-Path Entry to the Manifest,您需要通过添加具有适当配置的&lt;archive&gt; 元素来告诉Maven Jar 插件这样做。来自Manifest customization(略有改编):

    自定义清单

    可以更改默认清单 使用存档配置 元素。下面你会发现一些 配置选项是 可用的。有关更多信息,请参阅 Maven 存档参考。这个版本的 Maven JAR 插件使用 Maven Archiver 2.4.

    <project>
      ...
      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.3.1</version>
            <configuration>
              <archive>
                <index>true</index>
                <manifest>
                  <addClasspath>true</addClasspath>
                </manifest>
                <manifestEntries>
                  <mode>development</mode>
                  <url>${pom.url}</url>
                  <key>value</key>
                </manifestEntries>
              </archive>
            </configuration>
            ...
          </plugin>
        </plugins>
      </build>
      ...
    </project>
    

    但在您的情况下,问题是您正在(ab)使用 evil system scope,根据定义,它适用于应该始终可用且 [are] 不可用的依赖项在存储库中查找。所以不要指望 Maven 将它们放在 Manifest.mf 的 Class-Path 条目中。

    我猜I'll never repeat enough人们不应该使用它system作用域依赖,但他们的使用是strongly discouraged

    system:在项目生命周期的某个阶段需要此依赖项,但它是特定于系统的。 不鼓励使用此范围:这被认为是一种“高级”功能,只有在您真正了解其使用的所有后果时才应使用,如果不是实际上无法量化,这可能会非常困难。根据定义,此范围使您的构建不可移植。在某些边缘情况下可能是必要的。系统范围包括&lt;systemPath&gt; 元素,它指向本地机器上此依赖项的物理位置。因此,它用于指代预期会出现在给定本地机器上而不是存储库中的某些工件;并且其路径可能因机器而异。 systemPath 元素可以引用其路径中的环境变量:例如 ${JAVA_HOME}

    要么在本地存储库中安装 jar,要么使用公司存储库,或者使用 file based 存储库。但不要使用system 范围。

    【讨论】:

      【解决方案2】:

      maven compiler plugin 没有谈到一个名为“manifest”的配置属性,您可能想要使用 maven-jar-plugin。请参阅this link 了解如何操作。

      编辑: 在使用 pluginManagement 配置插件时,需要在元素中实际使用插件。 见documenttation

      <build>
         <pluginManagement>
            <plugins>
               <plugin>
                  <groupId>org.apache.maven.plugins</groupId>
                  <artifactId>maven-jar-plugin</artifactId>
                  <version>2.3.1</version>
                  <configuration>
                     <archive>
                        <manifest>
                           <addClasspath>true</addClasspath>
                        </manifest>
                        <manifestEntries>
                           <mode>development</mode>
                           <url>${pom.url}</url>
                           <key>value</key>
                        </manifestEntries>
                     </archive>
                  </configuration>
               </plugin>
           </plugins>
        </pluginManagement>
      
        <plugins>
            <!-- Use the jar plugin for plugin management configuration to take effect -->
            <plugin>
               <groupId>org.apache.maven.plugins</groupId>
               <artifactId>maven-jar-plugin</artifactId>
            </plugin>
        </plugins>
      </build>  
      

      在控制台类型上:

      $>mvn jar:jar
      

      编辑使用这个 pom.xml

      <project xmlns="http://maven.apache.org/POM/4.0.0" 
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
            xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
                  http://maven.apache.org/xsd/maven-4.0.0.xsd">
         <modelVersion>4.0.0</modelVersion>
      
         <groupId>com.adp.ihub</groupId>
         <artifactId>PreFinal</artifactId>
         <version>1</version>
         <packaging>jar</packaging>
      
         <name>PreFinal</name>
         <url>http://maven.apache.org</url>
      
         <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
         </properties>
      
         <dependencies>
            <dependency>
               <groupId>junit</groupId>
               <artifactId>junit</artifactId>
               <version>3.8.1</version>
               <scope>test</scope>
            </dependency>
      
            <!-- !!! ADD YOUR DEPENDENCIES HERE !!! -->
         </dependencies>
      
         <build>
            <pluginManagement>
               <plugins>
                  <plugin>
                     <groupId>org.apache.maven.plugins</groupId>
                     <artifactId>maven-jar-plugin</artifactId>
                     <version>2.3.1</version>
                     <configuration>
                        <archive>
                           <manifest>
                              <addClasspath>true</addClasspath>
                           </manifest>
                           <manifestEntries>
                              <mode>development</mode>
                              <url>${pom.url}</url>
                              <key>value</key>
                           </manifestEntries>
                        </archive>
                     </configuration>
                  </plugin>
               </plugins>
            </pluginManagement>
      
            <plugins>
               <plugin>
                  <groupId>org.apache.maven.plugins</groupId>
                  <artifactId>maven-jar-plugin</artifactId>
               </plugin>
            </plugins>
         </build>
      </project>
      

      【讨论】:

      • 我在下面添加了。还是一样的场景。 org.apache.maven.pluginsmaven-jar-plugin2.3.1truedevelopment${pom.url}value
      • 您将不得不使用 mvn jar:jar 命令或 mvn package 命令(如果您的项目是 jar),然后检查 jar 文件的内容。
      • 我只使用 mvn 包并检查了 jar 文件的内容。没用。
      • 你可以在你的问题中发布你的 pom.xml 的内容吗?
      • 好吧,你的 pom.xml 都错了,我很惊讶你的项目居然能编译。我已经用 pom.xml 更新了我的答案。使用它作为你的 pom,在依赖项部分添加所有依赖项。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-12-15
      • 2010-09-24
      • 2018-03-16
      • 2022-06-12
      • 2011-04-02
      • 2011-01-24
      • 2019-02-08
      相关资源
      最近更新 更多