2015-05-16 61 views
0

我正在写同一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生产仍不包含在pom.xml<dependencies>标签声明的依赖我jar。其实,我有以下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 

如何我可以包括依赖放进玻璃瓶以避免引发异常?

正如您从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 --- 
+0

@djeikyb你有没有读过我的问题?如果是这样,你会注意到我在'pom.xml'中包含了'maven-assembly-plugin'。我的问题是如何使该插件工作。 – user3663882

+0

你可以分享你如何运行?整个命令。 – Rekin

+0

我做到了!当我也了解这一点时,我犯了类似的错误。我强烈推荐阴影插件。它和旧的程序集插件方式都通过那里的答案来解决。 – djeikyb

回答

1
  1. 你错过了从装配配置执行。
  2. descriptorRef标签是指'预制'默认设置。你应该使用“desriptor”标签。
  3. 正在生成的程序集具有“.zip”扩展名,并包含其他jar文件,其中java不会自行解压缩。

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

+0

确实maven-shade插件解决了我所有的问题。谢谢! – user3663882