具有依赖项的可运行 jar

Runnable jar with dependencies

我正在编写同一个 AspectJ 项目 - github。我创建了以下 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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.badmitrii</groupId>
  <artifactId>test</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>test</name>
  <url>http://maven.apache.org</url>

    <dependencies>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.8.2</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.2</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjtools</artifactId>
            <version>1.8.2</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>aspectj-maven-plugin</artifactId>
                <version>1.7</version>
                <executions>
                    <execution>
                        <id>compile</id>
                        <goals>
                            <goal>compile</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <archive>
                        <addMavenDescriptor>false</addMavenDescriptor>
                        <manifestEntries>
                            <Class-Path>config/</Class-Path>
                        </manifestEntries>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                            <classpathLayoutType>custom</classpathLayoutType>
                            <customClasspathLayout>$${artifact.groupId}.$${artifact.artifactId}.$${artifact.extension}</customClasspathLayout>
                            <mainClass>com.badmitrii.Main</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>

            <plugin>
               <artifactId>maven-assembly-plugin</artifactId>
               <configuration>
                 <archive>
                   <manifest>


      <mainClass>com.badmitrii.Main</mainClass>
               </manifest>
             </archive>
             <descriptorRefs>
               <descriptorRef>assembly</descriptorRef>
             </descriptorRefs>
           </configuration>
        </plugin>
    </plugins>
</build>

assembly.xml

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">

    <id>assembly</id>

    <formats>
        <format>zip</format>
    </formats>

    <includeBaseDirectory>false</includeBaseDirectory>

    <dependencySets>
        <dependencySet>
            <outputDirectory>lib</outputDirectory>
            <outputFileNameMapping>${artifact.groupId}.${artifact.artifactId}.${artifact.extension}</outputFileNameMapping>
            <useProjectArtifact>false</useProjectArtifact>
            <!-- you may place excludes here -->
        </dependencySet>
    </dependencySets>

    <files>
        <file>
            <outputDirectory>/</outputDirectory>
            <source>${project.build.directory}/${project.artifactId}-${project.version}.jar</source>
            <destName>${project.artifactId}.jar</destName>
        </file>
    </files>

    <fileSets>
        <fileSet>
            <outputDirectory>config</outputDirectory>
            <directory>config</directory>
        </fileSet>
        <fileSet>
            <outputDirectory>/</outputDirectory>
            <directory>src/main/bin</directory>
        </fileSet>
    </fileSets>

</assembly>

但是 mvn install 生成的我的 jar 仍然不包含在 pom.xml 中的 <dependencies> 标记中声明的依赖项。实际上,我有以下 jar:

root
  |
  |--META-INF
  |
  |--com
  |   |
  |   |--badmitrii
  |          |
  |          |--Main.class
  |
  |--TestAspect.class
  |
  |--builddef.lst

当我尝试执行那个 jar 时,我得到

Exception in thread "main" java.lang.NoClassDefFoundError: org/aspectj/lang/NoAspectBoundException
        at com.badmitrii.Main.test(Main.java:1)
        at com.badmitrii.Main.main(Main.java:9)
Caused by: java.lang.ClassNotFoundException: org.aspectj.lang.NoAspectBoundException
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        ... 2 more

如何将依赖项包含到 jar 中以避免抛出异常?

正如您从 pom.xml 文件中看到的那样,我已经包含了 maven-assembly-plugin 声明,但它没有将依赖项包含到 jar 中。

我执行以下操作来编译和 运行 项目:

mvn install
java -jar ./target/test-1.0-SNAPSHOT.jar

好像连插件都没有运行。实际上 mvn install | grep 'maven' 打印以下内容:

[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ test ---
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ test ---
[INFO] --- aspectj-maven-plugin:1.7:compile (compile) @ test ---
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ test ---
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ test ---
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ test ---
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ test ---
[INFO] --- maven-install-plugin:2.4:install (default-install) @ test ---
  1. 您缺少程序集配置的执行。
  2. descriptorRef 标签引用 'prefabricated' 默认设置。您应该改用 "desriptor" 标签。
  3. 正在生成的程序集具有“.zip”扩展名并包含其他 jar 文件,java 不会自行解压。

我们可以继续并系统地纠正所有剩余的错误,但我们最终会得到 maven-shade-plugin 开箱即用的功能。你真的应该重新考虑使用它。