![](/img/trans.png)
[英]Maven doesn't see dependencies which are JAR files in a subfolder
[英]Maven dependencies doesn't get exported into the jar
我有一個要導出為jar的項目(由於某些原因,不可能將其導出為可運行的jar)。 我有3個maven依賴項, gson
, io
和junit
,但是當我在控制台中執行maven生成的jar時,它說:
檢查我的構建路徑:
我以這種方式導出它(Eclipse):以-> Maven build ...運行
(mvn) package
這是我的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>
據我了解,您需要的可能是這個
mvn install dependency:copy-dependencies
我的項目似乎沒有正確的項目結構,因此我創建了一個新的(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>
並執行mvn package
這樣就創建了一個帶有所有資源(圖像,接口fxml文件...)的“帶有依賴項的jar”。
生成的jar不包含依賴項,您必須在執行jar時在類路徑中提供它們,或者讓構建過程將其復制到jar中,從而創建所謂的uber-jar。 實現后者的好方法是使用maven-shade-plugin 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.