gpt4 book ai didi

java - 具有依赖项的可运行 jar

转载 作者:行者123 更新时间:2023-12-01 11:32:57 27 4
gpt4 key购买 nike

我正在写同样的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>

但是我的jarmvn install 制作仍然不包含 <dependencies> 中声明的依赖项标签 pom.xml 。实际上,我有以下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 标记指的是“预制”默认设置。您应该使用“desriptor”标签。
  3. 生成的程序集具有“.zip”扩展名,并包含其他 jar 文件,java 不会自行解压这些文件。

我们可以继续系统地纠正所有剩余的错误,但我们最终会得到与 maven-shade-plugin 开箱即用的效果。您确实应该重新考虑使用它。

关于java - 具有依赖项的可运行 jar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30272354/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com