网站首页 > 博客文章 正文
默认打包生成的jar是不能够直接运行的,因为带有main方法的类信息不会添加到manifest中(打开jar文件中的META-INF/MANIFEST.MF文件,将无法看到Main-Class一行)。
用maven打包java程序,当执行 java -jar 文件时提示 no main manifest attribute。
为了生成可执行的jar文件,需要借助插件。
目录:
生成示例项目
# 进入你想创建项目的父文件夹
cd /Volumes/RamDisk
# 查看当前文件夹
pwd
# 生成项目
docker run -itd --rm --name maven_quick_tmp \
-v "$HOME/.m2/repository":/root/.m2/repository \
-v "$PWD":/usr/src/mymaven \
-w /usr/src/mymaven \
virhuiai/maven_quick:version-aliyun \
mvn archetype:generate \
-DarchetypeArtifactId=maven-archetype-quickstart \
-DinteractiveMode=false \
-DarchetypeVersion=1.4 \
-DgroupId=com.virhuiai.www \
-DartifactId=hello-world \
-DpackageName=com.virhuiai.www \
-DarchetypeVersion=RELEASE
查看下生成的项目结构:
tree -C hello-world
其中pom.xml的部分如下:
如果要指定版本号,即将jdk版本替换为1.8:
cd hello-world/
sed -ri -e 's!<maven.compiler.source>1.7</maven.compiler.source>!<maven.compiler.source>1.8</maven.compiler.source>!g' pom.xml
sed -ri -e 's!<maven.compiler.target>1.7</maven.compiler.target>!<maven.compiler.target>1.8</maven.compiler.target>!g' pom.xml
方法一:maven-shade-plugin
官网地址在:
http://maven.apache.org/plugins/maven-shade-plugin/examples/executable-jar.html
按说明,在pom.xml中添加以下内容:
<build>
。。。
<pluginManagement>
。。。
</pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.virhuiai.www.App</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
注意这是直接位于build>plugins下的,不是pluginManagement里的,否则不会有效果()。
现在执行mvn clean install:
mvn clean install
其中有句:
Replacing /usr/src/mymaven/hello-world/target/hello-world-1.0-SNAPSHOT.jar with /usr/src/mymaven/hello-world/target/hello-world-1.0-SNAPSHOT-shaded.jar
说明已经被替换成带有Main-Class信息的可运行jar。
现在,在项目根目录中执行该jar文件:
root@9275e11b3f0f:/usr/src/mymaven/hello-world# java -jar /usr/src/mymaven/hello-world/target/hello-world-1.0-SNAPSHOT.jar
Hello World!
方法二:maven-jar-plugin
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.virhuiai.www.App</mainClass> <!-- //主程序入口类,可以按住control,单机定位到该类-->
</manifest>
</archive>
</configuration>
</plugin>
注意这也是直接位于build>plugins下的,不是pluginManagement里的,pluginManagement指定版本号。
猜你喜欢
- 2024-09-21 Java技术干货|利用Maven进行java项目构建应用常见功能
- 2024-09-21 MyEclipse中文教程六:新建Maven Web项目的步骤
- 2024-09-21 Java-Maven详解(maven javadoc)
- 2024-09-21 Maven如何统一管理团队后端项目开发技术栈?
- 2024-09-21 IntelliJ IDEA上创建Maven Spring MVC项目
- 2024-09-21 maven安装好了,接下来教你搭建ssm
- 2024-09-21 超全Maven常用命令(maven的使用教程)
- 2024-09-21 两分钟学会编写maven插件(maven 插件)
- 2024-09-21 Maven实战总结(maven从入门到精通)
- 2024-09-21 搭建Maven环境基础版(如何使用maven搭建项目)
你 发表评论:
欢迎- 最近发表
- 标签列表
-
- powershellfor (55)
- messagesource (56)
- aspose.pdf破解版 (56)
- promise.race (63)
- 2019cad序列号和密钥激活码 (62)
- window.performance (66)
- qt删除文件夹 (72)
- mysqlcaching_sha2_password (64)
- ubuntu升级gcc (58)
- nacos启动失败 (64)
- ssh-add (70)
- jwt漏洞 (58)
- macos14下载 (58)
- yarnnode (62)
- abstractqueuedsynchronizer (64)
- source~/.bashrc没有那个文件或目录 (65)
- springboot整合activiti工作流 (70)
- jmeter插件下载 (61)
- 抓包分析 (60)
- idea创建mavenweb项目 (65)
- vue回到顶部 (57)
- qcombobox样式表 (68)
- vue数组concat (56)
- tomcatundertow (58)
- pastemac (61)
本文暂时没有评论,来添加一个吧(●'◡'●)