简体   繁体   English

Maven依赖项不会导出到jar中

[英]Maven dependencies doesn't get exported into the jar

I have a project wich i want to export as jar (for some reasons it's impossible to export it as a runnable jar). 我有一个要导出为jar的项目(由于某些原因,不可能将其导出为可运行的jar)。 I have 3 maven dependencies, gson , io and junit , but when i execute the maven builded jar in console it says this: 我有3个maven依赖项, gsoniojunit ,但是当我在控制台中执行maven生成的jar时,它说:

在此处输入图片说明

Check my build path: 检查我的构建路径:

在此处输入图片说明

I export it this way (Eclipse): Run as -> Maven build... 我以这种方式导出它(Eclipse):以-> Maven build ...运行

(mvn) package

And here is my pom: 这是我的pom:

    <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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>Carlos</groupId>
  <artifactId>Buscaminas</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>Buscaminas</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
<build>
  <plugins>
    <plugin>
      <!-- Build an executable JAR -->
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-jar-plugin</artifactId>
      <version>3.0.2</version>
      <configuration>
        <archive>
          <manifest>
            <addClasspath>true</addClasspath>
            <classpathPrefix>lib/</classpathPrefix>
            <mainClass>res.application.Main</mainClass>
          </manifest>
        </archive>
      </configuration>
    </plugin>
  </plugins>
</build>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.2</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.5</version>
        </dependency>
    </dependencies>
</project>

Also the maven build result: 另外,Maven构建结果: 在此处输入图片说明

据我了解,您需要的可能是这个

mvn install dependency:copy-dependencies

My project didn't seem to have the correct project structure so I crated a new (maven) project and migrated my packages to the src/main/java folder and then used this: 我的项目似乎没有正确的项目结构,因此我创建了一个新的(maven)项目,然后将包迁移到src / main / java文件夹,然后使用此文件夹:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <executions>
                <execution>
                    <id>create-my-bundle</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                    <configuration>
                        <descriptorRefs>
                            <descriptorRef>jar-with-dependencies</descriptorRef>
                        </descriptorRefs>
                        <archive>
                            <manifest>
                                <mainClass>res.application.Main</mainClass> <!-- Or wherever is your main method-->
                            </manifest>
                        </archive>
                    </configuration>
                </execution>
            </executions>
        </plugin>

And executed mvn package 并执行mvn package

That created a "jar-with-dependencies" jar also with all the recources (images, interface fxml files...). 这样就创建了一个带有所有资源(图像,接口fxml文件...)的“带有依赖项的jar”。

The built jar does not contain the dependencies, you must either provide them in classpath when executing the jar or have build process copy them into your jar, creating a so-called uber-jar. 生成的jar不包含依赖项,您必须在执行jar时在类路径中提供它们,或者让构建过程将其复制到jar中,从而创建所谓的uber-jar。 Good way to achieve the latter is using maven-shade-plugin . 实现后者的好方法是使用maven-shade-plugin

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM