简体   繁体   English

maven程序集创建具有依赖项和类路径的jar

[英]maven assembly create jar with dependency and class path

I have a maven project with a lot of dependencies. 我有一个有很多依赖项的maven项目。 I want to pack all in a jar with dependency using assembly plugin, but I don't won't all the dependencies jars unpacked in a big mess. 我想使用程序集插件将所有依赖jar包装在一个带有依赖关系的jar中,但我并不是所有的依赖关系都不会在一个大混乱中解压缩。 I want all of them do go into lib folder, but I don't know how to add the class path. 我希望他们所有人都进入lib文件夹,但我不知道如何添加类路径。 my pom: 我的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>

my assembly file 我的程序集文件

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

He was probably reconfiguring his maven dependency plugin: 他可能正在重新配置他的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>

Now, when your run mvn package , all your dependency jars will go to target/lib (or whatever your distDirectory is). 现在,当你运行mvn package ,所有的依赖jar都将转到target/lib (或者你的distDirectory)。

HTH HTH

If maven-jar-plugin is not generating the entry Class-Path in the manifest file although you have set <addClasspath>true</addClasspath> , make sure you are using version 2.2 or newer of the plugin. 如果maven-jar-plugin没有在清单文件中生成条目Class-Path,尽管您已设置<addClasspath>true</addClasspath> ,请确保使用的是2.2或更新版本的插件。

It seems that the bug MJAR-83 in version 2.1 was preventing the plugin from listing the dependencies in Class-Path. 看来版本2.1中的错误MJAR-83阻止了插件在Class-Path中列出依赖项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM