【问题标题】:maven assembly create jar with dependency and class pathmaven 程序集创建具有依赖项和类路径的 jar
【发布时间】:2011-11-29 19:21:18
【问题描述】:

我有一个包含很多依赖项的 maven 项目。 我想使用程序集插件将所有依赖项打包在一个 jar 中,但我不会将所有依赖项 jar 解压成一团糟。我希望它们都进入 lib 文件夹,但我不知道如何添加类路径。 我的pom:

<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>test.com</groupId>
    <artifactId>simple-weather</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>simple-weather</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.14</version>
        </dependency>
        <dependency>
            <groupId>dom4j</groupId>
            <artifactId>dom4j</artifactId>
            <version>1.6.1</version>
        </dependency>
        <dependency>
            <groupId>jaxen</groupId>
            <artifactId>jaxen</artifactId>
            <version>1.1.1</version>
        </dependency>
        <dependency>
            <groupId>velocity</groupId>
            <artifactId>velocity</artifactId>
            <version>1.5</version>
        </dependency>
        <dependency>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>

                     <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>

            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <classpathPrefix>lib/</classpathPrefix>
                        <mainClass>adi.com.weather.Main</mainClass>
                    </manifest>
                </archive>
            </configuration>

        </plugin>

            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>attached</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <descriptors>
                        <descriptor>package.xml</descriptor>
                    </descriptors>
                </configuration>

            </plugin>


        </plugins>
    </build>
</project>

我的汇编文件

<?xml version="1.0" encoding="UTF-8"?>
<assembly>
    <id>test5</id>
    <formats>
        <format>jar</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <dependencySets>
        <dependencySet>
            <unpack>false</unpack>
            <scope>runtime</scope>
            <outputDirectory>lib</outputDirectory>
        </dependencySet>
    </dependencySets>
    <fileSets>
        <fileSet>
            <directory>${project.build.outputDirectory}</directory>
            <outputDirectory>/</outputDirectory>

        </fileSet>

    </fileSets>
</assembly>

【问题讨论】:

  • 看起来你有正确的 pom 条目。问题出在哪里?罐子不进入 lib 文件夹?清单的 jar 路径不正确?
  • manifest 中的 classPath 不能正常工作,但我找到了解决方案
  • 干得好阿迪莫尔。您能否自行发布该问题的答案,然后接受该答案以便我们结束该问题?此外,如果他们解决了您的问题,您需要接受之前问题的答案。
  • Adir Mor,解决办法是什么?
  • @Adi Mor 我也面临类似的问题。你是怎么解决的?

标签: java maven classpath maven-assembly-plugin


【解决方案1】:

他可能正在重新配置他的 maven 依赖插件:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>2.10</version>
            <executions>
                <execution>
                    <id>copy</id>
                    <phase>package</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${distDirectory}/lib</outputDirectory>
                        <includeScope>runtime</includeScope>
                    </configuration>
                </execution>
            </executions>
        </plugin>

现在,当您运行 mvn package 时,您所有的依赖 jar 都将转到 target/lib(或任何您的 distDirectory )。

HTH

【讨论】:

    【解决方案2】:

    如果 maven-jar-plugin 未在清单文件中生成条目 Class-Path,尽管您已设置 &lt;addClasspath&gt;true&lt;/addClasspath&gt;,请确保您使用的是 2.2 或更高版本的插件。

    似乎 2.1 版中的错误 MJAR-83 阻止了插件在 Class-Path 中列出依赖项。

    【讨论】:

      猜你喜欢
      • 2012-11-16
      • 1970-01-01
      • 2020-05-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-25
      • 1970-01-01
      • 2021-11-06
      相关资源
      最近更新 更多