pom中不包含任何引用的情况下,只需要在pom中添加 maven-jar-plugin
即可。
解决过程
新建项目,仅包含一个main函数类:xixi.java,输出 xixi say: hello
pom中配置为
1
2
3
4
5
6
7
8
9
10
11
|
<groupid>com.paxi</groupid>
<artifactid>xixi</artifactid>
<version> 1.0 -snapshot</version>
<packaging>jar</packaging>
<name>xixi</name>
<url>http: //maven.apache.org</url>
<properties>
<project.build.sourceencoding>utf- 8 </project.build.sourceencoding>
</properties>
|
执行 mvn clean install
生成jar包 xixi-1.0-snapshot.jar,
执行jar命令,执行反馈出错
1
2
|
java -jar xixi- 1.0 -snapshot.jar
xixi- 1.0 -snapshot.jar中没有主清单属性
|
对jar进行解压缩jar xvf xixi-1.0-snapshot.jar查看对应文件manifest.mf
1
2
3
4
5
6
|
cat meta-inf/manifest.mf
manifest-version: 1.0
archiver-version: plexus archiver
built-by: paxi
created-by: apache maven 3.3 . 3
build-jdk: 1.8 .0_121
|
在pom中添加插件 maven-jar-plugin
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<build>
<plugins>
<plugin>
<groupid>org.apache.maven.plugins</groupid>
<artifactid>maven-jar-plugin</artifactid>
<version> 3.1 . 0 </version>
<configuration>
<archive>
<manifest>
<mainclass>com.paxi.xixi</mainclass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
|
执行成功输出
java -jar xixi-1.0-snapshot.jar
xixi say: hello
再对解压缩后的manifest.mf文件查看为
1
2
3
4
5
6
|
cat meta-inf/manifest.mf
manifest-version: 1.0
built-by: paxi
created-by: apache maven 3.3 . 3
build-jdk: 1.8 .0_121
main- class : com.paxi.xixi //新增
|
在pom中添加依赖,maven如何生成可执行jar?
需要使用插件maven-dependency-plugin 和 maven-jar-plugin。
解决过程
在pom中仅仅增加一个apache-commons包
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
<groupid>com.paxi</groupid>
<artifactid>xixi</artifactid>
<version> 1.0 -snapshot</version>
<packaging>jar</packaging>
<name>xixi</name>
<url>http: //maven.apache.org</url>
<properties>
<project.build.sourceencoding>utf- 8 </project.build.sourceencoding>
</properties>
<dependencies>
<dependency>
<groupid>org.apache.commons</groupid>
<artifactid>commons-lang3</artifactid>
<version> 3.7 </version>
</dependency>
</dependencies>
|
在代码中运用common包中的一个类
1
2
3
4
5
6
7
|
public static void main(string[] args) {
string words = "xixi say: hello" ;
if (stringutils.isnotblank(words))
{
system.out.println(words);
}
}
|
打包后直接运行出错
java -jar xixi-1.0-snapshot.jar
exception in thread "main" java.lang.noclassdeffounderror: org/apache/commons/lang3/stringutils
at com.paxi.xixi.main(xixi.java:11)
caused by: java.lang.classnotfoundexception: org.apache.commons.lang3.stringutils
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
在pom中增加插件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
<build>
<plugins>
<plugin>
<groupid>org.apache.maven.plugins</groupid>
<artifactid>maven-jar-plugin</artifactid>
<version> 3.1 . 0 </version>
<configuration>
<archive>
<manifest>
<addclasspath> true </addclasspath>
<mainclass>com.paxi.xixi</mainclass>
<classpathprefix>libs/</classpathprefix>
</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}/libs
</outputdirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
|
再次运行
1
2
|
java -jar xixi- 1.0 -snapshot.jar
xixi say: hello
|
manifest.mf文件为
1
2
3
4
5
6
7
|
cat meta-inf/manifest.mf
manifest-version: 1.0
built-by: paxi
class -path: libs/commons-lang3- 3.7 .jar
created-by: apache maven 3.3 . 3
build-jdk: 1.8 .0_121
main- class : com.paxi.xixi
|
不仅使用了pom中的依赖还使用了三方的jar,maven如何生成可执行jar?
编译过程需要对编译插件 maven-compiler-plugin
做修改,运行的时候需要加上三方jar的目录,执行命令
1
2
3
|
java -jar -djava.ext.dirs=lib/ target/xixi- 1.0 -snapshot.jar
xixi say: hello
papa say:hello
|
编译解决过程
测试代码为
1
2
3
4
5
6
7
8
|
public static void main(string[] args) {
string words = "xixi say: hello" ;
if (stringutils.isnotblank(words))
{
system.out.println(words);
}
papa.say();
}
|
使用mvn命令执行报错
[error] failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project xixi: compilation failure
xixi/src/main/java/com/paxi/xixi.java:[15,9] 找不到符号
pom中添加编译插件
1
2
3
4
5
6
7
8
9
10
|
<plugin>
<groupid>org.apache.maven.plugins</groupid>
<artifactid>maven-compiler-plugin</artifactid>
<version> 3.1 </version>
<configuration>
<compilerargs>
<arg>-djava.ext.dirs=lib/</arg>
</compilerargs>
</configuration>
</plugin>
|
打包执行
1
2
3
4
5
6
7
8
9
10
|
java -jar target/xixi- 1.0 -snapshot.jar
xixi say: hello
exception in thread "main" java.lang.noclassdeffounderror: com/paxi/papa
at com.paxi.xixi.main(xixi.java: 15 )
caused by: java.lang.classnotfoundexception: com.paxi.papa
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
|
加上参数后即可得出结果
1
2
3
|
java -jar -djava.ext.dirs=lib/ target/xixi- 1.0 -snapshot.jar
xixi say: hello
papa say:hello
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://segmentfault.com/a/1190000015372200