我正在编写同一个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 ---
答案 0 :(得分:1)
我们可以继续并系统地纠正所有剩余的错误,但我们最终会完全使用maven-shade-plugin开箱即用的内容。你真的应该重新考虑使用它。