[英]Creating jar by maven to read resources text file from the same jar file
我在Eclipse中有一個簡單的Maven項目。 該應用程序包含文件:src / main / resources / test.txt。 我想讀取文件並將其寫入輸出。 它在Eclipse中有效,但在創建jar文件后不能從命令行使用。 如何配置項目以兩種方式工作。 我哪里錯了? 不正確的jar文件,java代碼,清單,maven配置?
應用結構:
正在運行:
java -jar fileRead-0.0.1-SNAPSHOT.jar
錯誤:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/io/IOUtils
at fileRead.FileRead.main(FileRead.java:11)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.io.IOUtils
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 1 more
Java代碼:
package fileRead;
import java.io.IOException;
import org.apache.commons.io.IOUtils;
public class FileRead {
public static void main(String[] args) {
String file = null;
try {
file = IOUtils.toString((new FileRead()).getClass().getClassLoader().getResourceAsStream("test.txt"));
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(file);
}
}
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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.fileRead</groupId>
<artifactId>fileRead</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>fileRead</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.directory.studio</groupId>
<artifactId>org.apache.commons.io</artifactId>
<version>2.4</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>fileRead.FileRead</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
fileRead-0.0.1-SNAPSHOT.jar:
fileRead
FileRead.class
META-INF
maven
MANIFEST.MF
test.txt
MANIFEST.MF:
Manifest-Version: 1.0
Built-By: msmorenda
Class-Path: org.apache.commons.io-2.4.jar commons-io-2.4.jar
Created-By: Apache Maven 3.5.0
Build-Jdk: 1.8.0_121
Main-Class: fileRead.FileRead
test.txt:
abc
def
ghi
問題是MANIFEST.MF
指向所需的依賴項。 但是,依賴項與生成的jar文件不在同一目錄中。 要復制所需的庫(commons-io),您需要如下所示將另一個插件添加到pom中:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<archive>
<manifest>
<classpathPrefix>lib</classpathPrefix>
<addClasspath>true</addClasspath>
<mainClass>fileread.FileRead</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
具有以上配置的maven-dependency-plugin
會將所需的庫復制到target
下的lib
目錄中。 現在,如果將target
目錄作為當前目錄,並調用java -jar <name_of_jar>
一切正常。
注意:如果要將jar文件移動到其他位置,則應將其與lib
目錄打包在一起。 而且還要注意,我改變了你的類的包名fileread
而不是fileRead
。 原因? Java約定。
您可以使用包含所需庫的maven創建一個程序集,然后使用“ mvn程序包”進行創建
pom.xml
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.6</version>
<configuration>
<descriptors>
<descriptor>src/assemblies/bin-with-dependencies.xml</descriptor>
</descriptors>
<tarLongFileMode>gnu</tarLongFileMode>
</configuration>
</plugin>
</plugins>
</build>
src / assemblies / bin-with-dependencies.xml
<assembly>
<id>bin-with-dependencies</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<outputDirectory></outputDirectory>
<unpack>false</unpack>
<scope>compile</scope>
</dependencySet>
<dependencySet>
<unpack>false</unpack>
<scope>runtime</scope>
<useProjectArtifact>true</useProjectArtifact>
</dependencySet>
</dependencySets>
</assembly>
這將創建並存檔一個包含jar的文件夾及其所有庫,然后像運行它一樣
java -cp {directory_jars} / * fileread.FileRead ...
“ / *”可能僅適用於Windows
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.