我已经在eclipse上开发了一个maven项目,并在其上运行良好。我想从中构建一个jar并从命令提示符运行它。我不想要一个包含所有依赖项的大jar。我想要一个带有项目代码的小jar,它使用'lib'文件夹作为依赖项(使用jar文件)和'resources'文件夹作为属性文件。
我在Maven中使用的插件:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>${jdk.version}</source>
<target>${jdk.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.1</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathLayoutType>custom</classpathLayoutType>
<customClasspathLayout>lib/${artifact.artifactId}${artifact.dashClassifier?}-${artifact.version}.${artifact.extension}</customClasspathLayout>
<mainClass>mypkg.Main</mainClass>
</manifest>
<manifestEntries>
<Class-Path>. ${project.build.directory}\resources ${project.build.directory}\lib</Class-Path>
</manifestEntries>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}\lib</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>copy-resources</id>
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}\resources</outputDirectory>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
我的主要课程:
package mypkg;
import java.io.IOException;
public class Main {
public static void main(String[] args)
throws InstantiationException, IllegalAccessException, ClassNotFoundException, IOException {
.......
.......
}
}
使用“META-INF”文件夹中的清单文件创建jar。 清单文件(删除了类路径中的一些jar条目以供查看):
Manifest-Version: 1.0
Built-By: userName
Class-Path: . D:/project2/mypkg/target/ D:/project2/mypkg/target/resources D:/project2/mypkg/target/lib lib/hive-jdbc.jar lib/hive-common.jar lib/commons-lang.jar
Created-By: Apache Maven 3.3.9
Build-Jdk: 1.8.0_101
Main-Class: mypkg.Main
文件夹结构:
folder-
-lib
-(all the jar files)
-resources
-(properties file)
-application.jar
我在命令提示符下使用'java -jar application.jar mypkg.Main'。
但它显示'错误:无法找到或加载主类mypkg.Main'
有人可以告诉我导致错误的原因以及解决方法吗?
答案 0 :(得分:1)
Finally found the solution. Just need to do some modifications in the manifest file. The versions of the jars were missing.
Final Manifest file(removed some jar entries in classpath for viewing purpose)::
Manifest-Version: 1.0
Built-By: userName
Class-Path: lib/hive-jdbc-1.1.0.jar lib/hive-common-1.1.0.jar
Created-By: Apache Maven 3.3.9
Build-Jdk: 1.8.0_101
Main-Class: mypkg.Main
Just a note: Wild cards for example "lib/*" did not work in the 'Class-Path' header in the Manifest file (Java 6 and above).