使用maven打包jar_两个java文件打包成jar

使用maven打包jar_两个java文件打包成jar目录打包方法方法一:使用maven-jar-plugin和maven-dependency-plugin方法二:使用maven-assembly-plugin(推荐)方法三:使用maven-shade-plugin方法四:使用onejar-maven-plugin方法五:使用spring-boot-maven-plugin方法六:使用tomcat7-maven-plugin参考打包方法方法一:使用maven-jar-plugin和maven-dependenc.

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE稳定放心使用

目录

打包方法

方法一:使用maven-jar-plugin和maven-dependency-plugin

方法二:使用maven-assembly-plugin (推荐)

方法三:使用maven-shade-plugin

方法四:使用onejar-maven-plugin

方法五:使用spring-boot-maven-plugin

方法六:使用tomcat7-maven-plugin

参考


 

打包方法

方法一:使用maven-jar-pluginmaven-dependency-plugin

首先,maven-jar-plugin的作用是配置mainClass和指定classpath。

 

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <configuration>
        <archive>
            <manifest>
                <addClasspath>true</addClasspath>
                <classpathPrefix>libs/</classpathPrefix>
                <mainClass>
                    org.baeldung.executable.ExecutableMavenJar
                </mainClass>
            </manifest>
        </archive>
    </configuration>
</plugin>

addClasspath: 是否在manifest文件中添加classpath。默认为false。如果为true,则会在manifest文件中添加classpath,这样在启动的时候就不用再手动指定classpath了。如下所示,文件中增加了Class-Path一行

 

Manifest-Version: 1.0                                                                                                                                         
Archiver-Version: Plexus Archiver
Built-By: michealyang
Class-Path: libs/jetty-server-9.4.7.v20170914.jar lib/javax.servlet-api
 -3.1.0.jar libs/jetty-http-9.4.7.v20170914.jar 
Created-By: Apache Maven 3.3.9
Build-Jdk: 1.8.0_162-ea
Main-Class: com.michealyang.jetty.embeded.EmbeddedJettyServer

classpathPrefix: classpath的前缀。如上面的manifest文件中,Class-Path的值中,每个jar包的前缀都是libs/。本质上,这个配置的值是所依赖jar包所在的文件夹。配置正确了才能找到依赖
mainClass: 指定启动时的Main Class

其次,maven-dependency-plugin会把所依赖的jar包copy到指定目录

 

<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}/libs
                </outputDirectory>
            </configuration>
        </execution>
    </executions>
</plugin>

executions中的配置都很重要,按照上面的配置来就行了。outputDirectory指定了要将所依赖的jar包copy到哪个目录。要与maven-jar-plugin中的classpathPrefix一致。

执行如下命令,即可打包:

mvn package

打包结果是,自己写的Class在jar包中,所依赖的jar包在libs目录中:

├── embedded-jetty-1.0.0-SNAPSHOT.jar
├── lib
│ ├── jetty-server-9.4.7.v20170914.jar
│ ├── jetty-http-9.4.7.v20170914.jar

执行如下命令即可启动jar包:

java -jar embedded-jetty-1.0.0-SNAPSHOT.jar

优点
有诸多配置项,很自由,每个步骤都可控

缺点
打成的最终jar包中没有所依赖的jar包。依赖跟自己的代码不在一个jar包中。部署或者移动的时候,要考虑到多个文件,比较麻烦

方法二:使用maven-assembly-plugin (推荐)

maven-assembly-plugin可以将所有的东西都打包到一个jar包中。

 

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
            <configuration>
                <archive>
                <manifest>
                    <mainClass>
                        com.michealyang.jetty.embeded.EmbeddedJettyServer
                    </mainClass>
                </manifest>
                </archive>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
        </execution>
    </executions>
</plugin>

执行mvn package后,会在target文件夹下生成两个jar包,一个是不带依赖的jar包,一个是后缀有-dependencies带有依赖的jar包,如:

May 31 16:42 embedded-jetty-1.0.0-SNAPSHOT-jar-with-dependencies.jar
May 31 16:42 embedded-jetty-1.0.0-SNAPSHOT.jar

启动时,直接执行即可:

java -jar embedded-jetty-1.0.0-SNAPSHOT-jar-with-dependencies.jar

优点
所有的东西都打到一个jar包中,很方便
缺点
配置项少,不自由。

方法三:使用maven-shade-plugin

maven-assembly-plugin类似,都可以将所有的东西都打包到一个jar包中。

 

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <executions>
        <execution>
            <goals>
                <goal>shade</goal>
            </goals>
            <configuration>
                <shadedArtifactAttached>true</shadedArtifactAttached>
                <transformers>
                    <transformer implementation=
                      "org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                        <mainClass>com.michealyang.jetty.embeded.EmbeddedJettyServer</mainClass>
                </transformer>
            </transformers>
        </configuration>
        </execution>
    </executions>
</plugin>

执行mvn package后,会在target文件夹下生成两个jar包,一个是不带依赖的jar包,一个是后缀有-shaded带有依赖的jar包,如:

May 31 16:53 embedded-jetty-1.0.0-SNAPSHOT-shaded.jar
May 31 16:53 embedded-jetty-1.0.0-SNAPSHOT.jar

启动时,直接执行即可:

java -jar embedded-jetty-1.0.0-SNAPSHOT-jar-with-shaded.jar

优点
功能同maven-assembly-plugin,但比前者强大
缺点
配置起来太麻烦。当你需要高级功能的时候,更是麻烦的不要不要的。

方法四:使用onejar-maven-plugin

This provides custom classloader that knows how to load classes and resources from jars inside an archive, instead of from jars in the filesystem.

 

<plugin>
    <groupId>com.jolira</groupId>
    <artifactId>onejar-maven-plugin</artifactId>
    <executions>
        <execution>
            <configuration>
                <mainClass>org.baeldung.executable.
                  ExecutableMavenJar</mainClass>
                <attachToBuild>true</attachToBuild>
                <filename>
                  ${project.build.finalName}.${project.packaging}
                </filename>
            </configuration>
            <goals>
                <goal>one-jar</goal>
            </goals>
        </execution>
    </executions>
</plugin>

优点
clean delegation model, allows classes to be at the top-level of the One Jar, supports external jars and can support Native libraries
缺点
not actively supported since 2012

方法五:使用spring-boot-maven-plugin

能同时打可执行jar包和war包
This allows to package executable jar or war archives and run an application “in-place”.

需要maven版本不低于3.2

 

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <executions>
        <execution>
            <goals>
                <goal>repackage</goal>
            </goals>
            <configuration>
                <classifier>spring-boot</classifier>
                <mainClass>
                  org.baeldung.executable.ExecutableMavenJar
                </mainClass>
            </configuration>
        </execution>
    </executions>
</plugin>

两个重点:

  • goal要写成repackage
  • classifier要写成spring-boot

优点
dependencies inside a jar file, you can run it in every accessible location, advanced control of packaging your artifact, with excluding dependencies from the jar file etc., packaging of war files as well

缺点
添加了一些不必要的Spring和Spring Boot依赖

方法六:使用tomcat7-maven-plugin

可打包成一个web工程类型的jar包。其实是内嵌了一个tomcat在里面。

 

<plugin>
    <groupId>org.apache.tomcat.maven</groupId>
    <artifactId>tomcat7-maven-plugin</artifactId>
    <version>2.0</version>
    <executions>
        <execution>
            <id>tomcat-run</id>
            <goals>
                <goal>exec-war-only</goal>
            </goals>
            <phase>package</phase>
            <configuration>
                <path>/</path>
                <enableNaming>false</enableNaming>
                <finalName>webapp.jar</finalName>
                <charset>utf-8</charset>
            </configuration>
        </execution>
    </executions>
</plugin>

The goal is set as exec-war-only, path to your server is specified inside configuration tag, with additional properties, like finalName, charset etc. To build a jar, run man package, which will result in creating webapp.jar in your target directory. To run

To run the application, just write this in your console: java -jar target/webapp.jar and try to test it by specifying the localhost:8080/ in a browser.

优点
只有一个jar包
缺点
打包出的文件很大。因为里面内嵌了一个tomcat

参考

How to Create an Executable JAR with Maven

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/185897.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

  • 苹果app测试_ios上架app费用

    苹果app测试_ios上架app费用详细操作地址http://www.applicationloader.net/blog/zh/88.html苹果iOSAPP真机调试测试和上架AppStore视频教程优酷http://v.youku.com/v_show/id_XMzk0MTMyNDM2NA==.html?spm=a2hzp.8244740.0.0转载于:https://www.cnblogs.com/…

  • UVa 10054 : The Necklace 【欧拉回路】

    UVa 10054 : The Necklace 【欧拉回路】

  • linux的vi命令详解_centos7 vi命令

    linux的vi命令详解_centos7 vi命令Linux命令-vi命令  vi编辑器是所有Unix及Linux系统下标准的编辑器,它的强大不逊色于任何最新的文本编辑器.由于对Unix及Linux系统的任何版本,vi编辑器是完全相同的,因此您可以在其他任何介绍vi的地方进一步了解它。Vi也是Linux中最基本的文本编辑器.。1.语法:vi[参数][文件名称]…2.功能:  编辑文件。3.参数:n打印最近的n条历史命令。-N显示历史记录中最近的N个记录。-c清空当前历史命令。-a将目前新增的历史

  • C#单纯的字母数字ASCII码转换

    字母转换成数字byte[]array=newbyte[1];//定义一组数组arrayarray=System.Text.Encoding.ASCII.GetBytes(string

    2021年12月27日
  • 支持向量机与支持向量回归(support vector machine and support vector regression)

    支持向量机与支持向量回归(support vector machine and support vector regression)支持向量机和支持向量回归是目前机器学习领域用得较多的方法,不管是人脸识别,字符识别,行为识别,姿态识别等,都可以看到它们的影子。在我的工作中,经常用到支持向量机和支持向量回归,然而,作为基本的理论,却没有认真地去梳理和总结,导致有些知识点没有彻底的弄明白。这篇博客主要就是想梳理一遍支持向量机和支持向量回归的基础理论知识,一个是笔记,另一个是交流学习,便于大家共勉。凸集、凸函数、凸优化凸集:如果集合…

  • linux17:时间间隔计算脚本练习——距离你的生日还有多少天

    linux17:时间间隔计算脚本练习——距离你的生日还有多少天需求距离一个未来的日子还有多少天距离一个过去的日子过去多少天#!/bin/bash#name——timeDistance.sh#function#input a date, to calculate how many days to this day#2022.1.18 daxiongread -p “please input your date like YYYYMMDD ex>20150716 : ” date2#test whether or not have

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号