Java: 使用依赖项运行jar

huangapple go评论52阅读模式
英文:

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:

&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
  xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd&quot;&gt;
  &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
  &lt;groupId&gt;me.jeusdi.slab&lt;/groupId&gt;
  &lt;artifactId&gt;slab&lt;/artifactId&gt;
  &lt;packaging&gt;jar&lt;/packaging&gt;
  &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;
  &lt;name&gt;slab&lt;/name&gt;
  &lt;url&gt;http://maven.apache.org&lt;/url&gt;

  &lt;properties&gt;
    &lt;kafka.streams.version&gt;3.4.0&lt;/kafka.streams.version&gt;
    &lt;spring.version&gt;6.0.5&lt;/spring.version&gt;

    &lt;maven.compiler.source&gt;17&lt;/maven.compiler.source&gt;
    &lt;maven.compiler.target&gt;17&lt;/maven.compiler.target&gt;
  &lt;/properties&gt;
  
  &lt;dependencies&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;org.apache.kafka&lt;/groupId&gt;
        &lt;artifactId&gt;kafka-streams&lt;/artifactId&gt;
        &lt;version&gt;${kafka.streams.version}&lt;/version&gt;
    &lt;/dependency&gt;

    &lt;!-- Spring Core --&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;org.springframework&lt;/groupId&gt;
        &lt;artifactId&gt;spring-core&lt;/artifactId&gt;
        &lt;version&gt;${spring.version}&lt;/version&gt;
    &lt;/dependency&gt;

    &lt;!-- Spring Context --&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;org.springframework&lt;/groupId&gt;
        &lt;artifactId&gt;spring-context&lt;/artifactId&gt;
        &lt;version&gt;${spring.version}&lt;/version&gt;
    &lt;/dependency&gt;

  &lt;/dependencies&gt;

    &lt;build&gt;
        &lt;plugins&gt;
            &lt;plugin&gt;
                &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
                &lt;version&gt;3.10.1&lt;/version&gt;
                &lt;configuration&gt;
                    &lt;source&gt;${maven.compiler.source}&lt;/source&gt;
                    &lt;target&gt;${maven.compiler.target}&lt;/target&gt;
                &lt;/configuration&gt;
            &lt;/plugin&gt;
        &lt;/plugins&gt;
    &lt;/build&gt;
&lt;/project&gt;

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.

        &lt;plugin&gt;
            &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
            &lt;artifactId&gt;maven-jar-plugin&lt;/artifactId&gt;
            &lt;version&gt;3.2.0&lt;/version&gt;
            &lt;configuration&gt;
                &lt;outputDirectory&gt;${project.build.directory}/dist&lt;/outputDirectory&gt;
                &lt;archive&gt;
                    &lt;manifest&gt;
                        &lt;addDefaultImplementationEntries&gt;true&lt;/addDefaultImplementationEntries&gt;
                        &lt;addClasspath&gt;true&lt;/addClasspath&gt;
                        &lt;classpathPrefix&gt;lib&lt;/classpathPrefix&gt;
                        &lt;mainClass&gt;${mainclass}&lt;/mainClass&gt;
                    &lt;/manifest&gt;
                &lt;/archive&gt;
            &lt;/configuration&gt;
        &lt;/plugin&gt;
        &lt;plugin&gt;
            &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
            &lt;artifactId&gt;maven-dependency-plugin&lt;/artifactId&gt;
            &lt;version&gt;3.1.1&lt;/version&gt;
            &lt;executions&gt;
                &lt;execution&gt;
                    &lt;id&gt;copy-dependencies&lt;/id&gt;
                    &lt;phase&gt;package&lt;/phase&gt;
                    &lt;goals&gt;
                        &lt;goal&gt;copy-dependencies&lt;/goal&gt;
                    &lt;/goals&gt;
                    &lt;configuration&gt;
                        &lt;outputDirectory&gt;${project.build.directory}/dist/lib&lt;/outputDirectory&gt;
                        &lt;overWriteReleases&gt;false&lt;/overWriteReleases&gt;
                        &lt;overWriteSnapshots&gt;false&lt;/overWriteSnapshots&gt;
                        &lt;overWriteIfNewer&gt;true&lt;/overWriteIfNewer&gt;
                    &lt;/configuration&gt;
                &lt;/execution&gt;
            &lt;/executions&gt;
        &lt;/plugin&gt;

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

只需将&lt;mainClass&gt;fully.qualified.MainClass&lt;/mainClass&gt;中的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

&lt;plugin&gt;
      &lt;artifactId&gt;maven-assembly-plugin&lt;/artifactId&gt;
      &lt;configuration&gt;
        &lt;archive&gt;
          &lt;manifest&gt;
            &lt;mainClass&gt;fully.qualified.MainClass&lt;/mainClass&gt;
          &lt;/manifest&gt;
        &lt;/archive&gt;
        &lt;descriptorRefs&gt;
          &lt;descriptorRef&gt;jar-with-dependencies&lt;/descriptorRef&gt;
        &lt;/descriptorRefs&gt;
      &lt;/configuration&gt;
    &lt;/plugin&gt;

Just replace fully.qualified.MainClass in
&lt;mainClass&gt;fully.qualified.MainClass&lt;/mainClass&gt;
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

huangapple
  • 本文由 发表于 2023年2月24日 01:34:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/75548344.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定