具有依赖关系的Runnable jar

时间:2015-05-16 05:58:22

标签: java maven jar

我正在编写同一个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>

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

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

当我尝试执行那个罐子时,我得到了

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 个答案:

答案 0 :(得分:1)

  1. 您从配件配置中缺少执行。
  2. descriptorRef标签是指预制的&#39;默认设置。你应该使用&#34; desriptor&#34;而是标记。
  3. 正在生成的程序集有&#34; .zip&#34;扩展并包含其他jar文件,java不会自行解包。
  4. 我们可以继续并系统地纠正所有剩余的错误,但我们最终会完全使用maven-shade-plugin开箱即用的内容。你真的应该重新考虑使用它。