英文:
Java: run jar with dependencies
问题
以下是您要翻译的内容:
I've packaged my mvn project.
我已经打包了我的Maven项目。
After that, my jar
files only contains my hand coded classes:
之后,我的jar
文件只包含了我手写的类:
META-INF/
META-INF/MANIFEST.MF
me/
me/jeusdi/
me/jeusdi/slab/
me/jeusdi/slab/MyService.class
me/jeusdi/slab/App.class
me/jeusdi/slab/MyConfig.class
me/jeusdi/slab/MyServiceImpl.class
META-INF/maven/
META-INF/maven/me.jeusdi.slab/
META-INF/maven/me.jeusdi.slab/slab/
META-INF/maven/me.jeusdi.slab/slab/pom.xml
META-INF/maven/me.jeusdi.slab/slab/pom.properties
Nevertheless, I need declared maven dependencies:
然而,我需要声明的Maven依赖项:
<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>me.jeusdi.slab</groupId>
<artifactId>slab</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>slab</name>
<url>http://maven.apache.org</url>
<properties>
<kafka.streams.version>3.4.0</kafka.streams.version>
<spring.version>6.0.5</spring.version>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-streams</artifactId>
<version>${kafka.streams.version}</version>
</dependency>
<!-- Spring Core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- Spring Context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
Why spring-core
dependency is not present into my jar.
为什么spring-core
依赖项不包含在我的jar
文件中。
I need to run my jar. Currently, I'm using:
我需要运行我的jar
文件。目前,我正在使用:
java -cp target/slab-1.0-SNAPSHOT.jar App
Any ideas about how to embed or locate my dependencies and run my application?
关于如何嵌入或定位我的依赖项并运行应用程序,有任何想法吗?
英文:
I've packaged my mvn project.
After that, my jar
files only contains my hand coded classes:
META-INF/
META-INF/MANIFEST.MF
me/
me/jeusdi/
me/jeusdi/slab/
me/jeusdi/slab/MyService.class
me/jeusdi/slab/App.class
me/jeusdi/slab/MyConfig.class
me/jeusdi/slab/MyServiceImpl.class
META-INF/maven/
META-INF/maven/me.jeusdi.slab/
META-INF/maven/me.jeusdi.slab/slab/
META-INF/maven/me.jeusdi.slab/slab/pom.xml
META-INF/maven/me.jeusdi.slab/slab/pom.properties
Nevertheless, I need declared maven dependencies:
<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>me.jeusdi.slab</groupId>
<artifactId>slab</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>slab</name>
<url>http://maven.apache.org</url>
<properties>
<kafka.streams.version>3.4.0</kafka.streams.version>
<spring.version>6.0.5</spring.version>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-streams</artifactId>
<version>${kafka.streams.version}</version>
</dependency>
<!-- Spring Core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- Spring Context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
Why spring-core
dependency is not present into my jar.
I need to run my jar. Currently, I'm using:
java -cp target/slab-1.0-SNAPSHOT.jar App
Any ideas about how to embed or locate my dependencies and run my application?
答案1
得分: 1
你可以使用Maven创建一个包含所有类的超级JAR文件。你还可以要求Maven为你下载依赖项。
通常我会生成与你一样的JAR文件,然后自动下载所有依赖项到一个lib文件夹中。为了使运行JAR文件像"java -jar ..."一样简单,我甚至让Maven将类路径添加到清单中。唯一的缺点是现在你需要以完全相同的目录结构分发JAR文件及其库。好处是任何已签名的JAR文件都会保留其签名,因此有效。
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<outputDirectory>${project.build.directory}/dist</outputDirectory>
<archive>
<manifest>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
<addClasspath>true</addClasspath>
<classpathPrefix>lib</classpathPrefix>
<mainClass>${mainclass}</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.1.1</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/dist/lib</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
英文:
You can use Maven to create an uber-jar that contains all the classes.
You can as well ask Maven to download the dependencies for your.
What I usually do is to generate the jar the same as you do, but then automatically download all the dependencies into a lib folder.
To make running the jar as easy as java -jar ...
I have Maven even add the classpath into the manifest.
The only drawback is that you now need to distribute the jar and it's libs in exactly this directory structure.
The advantage is that any signed jar will preserve it's signature and thus validity.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<outputDirectory>${project.build.directory}/dist</outputDirectory>
<archive>
<manifest>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
<addClasspath>true</addClasspath>
<classpathPrefix>lib</classpathPrefix>
<mainClass>${mainclass}</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.1.1</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/dist/lib</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
答案2
得分: 1
如果您想将具有依赖项的JAR 包含在其中,可以添加以下内容
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>fully.qualified.MainClass</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
只需将<mainClass>fully.qualified.MainClass</mainClass>
中的fully.qualified.MainClass
替换为具有main
方法的类名。
要构建它,您可以运行
mvn clean compile assembly:single
有关更多详细信息,请参阅 https://stackoverflow.com/a/574650/5622596。
至于如何运行JAR 文件,一种可能的方法是运行以下命令
java -jar NameOfJar.jar
只需将NameOfJar.jar
替换为您创建的JAR 文件的名称。
英文:
If you want to include the jar with dependencies you could add the following
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>fully.qualified.MainClass</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
Just replace fully.qualified.MainClass in
<mainClass>fully.qualified.MainClass</mainClass>
with which ever class has main
To Build it you can run
mvn clean compile assembly:single
See https://stackoverflow.com/a/574650/5622596 for more details.
As to how to run a jar one possible way is to run the following
java -jar NameOfJar.jar
Just replace NameOfJar.jar
with whichever jar you created
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论