首页 > 其他分享 >IDEA+Maven 打jar包

IDEA+Maven 打jar包

时间:2023-04-28 11:58:47浏览次数:43  
标签:xml maven gt jar IDEA Maven lt

IDEA+Maven 打jar包
原文链接:https://blog.csdn.net/qq_46416934/article/details/124146569

IDEA+Maven 打jar包 (包涵依赖jar)

最新修改
以前写的打包方式可能看起来有些繁琐,这里给出进一步简洁的解决方案简单粗暴

写在前面:

这两天一直在整(gu)理(dao)IDEA 用Maven打jar包,网上的教程是各式各样,但是都不能满足我的需求(或者 还没有找个正确的),因此 综合网上的内容 自己整理了一下(以下内容是在mac系统下 win 可能有一些地方不一样)。

软件环境:

IDEA:2017.1.5

Maven:3.3.9

打jar包:

  • IDEA 自带的打包工具使用

    • 打无依赖jar包

      1. 如果工程目录中有以下文件夹 删掉
        这里写图片描述
    1. 点击工程上方【File】菜单选择【Project Structure】
      这里写图片描述
    2. 然后就会生成一个META-INF的文件夹,里面就是一些配置属性
    • 打包涵依赖jar包
      对以上的方式中的步骤6中 不做任何修改,然后执行步骤7

  • Maven 打包
    • 打无依赖jar包

      1. 在pom.xml 文件里加入

         <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-jar-plugin</artifactId>
              <version>2.4</version>
              <configuration>
                <archive>
                      生成的jar中,不要包含pom.xml和pom.properties这两个文件
                  <addMavenDescriptor>false</addMavenDescriptor>
                  <manifest>
                        是否要把第三方jar放到manifest的classpath中
                    <addClasspath>true</addClasspath>
                       生成的manifest中classpath的前缀,因为要把第三方jar放到lib目录下,所以classpath的前缀是lib/
                    <classpathPrefix>lib/</classpathPrefix>
                        应用的main class
                    <mainClass>com.yourClass</mainClass>
                  </manifest>
                </archive>
                    过滤掉不希望包含在jar中的文件
                <excludes>
                  <exclude>${project.basedir}/xml/*</exclude>
                </excludes>
             </configuration>
         </plugin> 
        
        • 1
        • 2
        • 3
        • 4
        • 5
        • 6
        • 7
        • 8
        • 9
        • 10
        • 11
        • 12
        • 13
        • 14
        • 15
        • 16
        • 17
        • 18
        • 19
        • 20
        • 21
        • 22
        • 23
      2. 点开Maven管理工具
        这里写图片描述

    • 打包涵依赖jar包
      第一种方法
      1. 在pom.xml文件中写入

        <build>
           <plugins>
            <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-compiler-plugin</artifactId>
              <version>3.1</version>
            </plugin>
            <plugin>
              <artifactId>maven-assembly-plugin</artifactId>
              <configuration>
                <archive>
                  <manifest>
                    <mainClass>com.test.app</mainClass>
                  </manifest>
                </archive>
                <descriptorRefs>
                  <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
              </configuration>
              <!--下面是为了使用 mvn package命令,如果不加则使用mvn assembly-->
              <executions>
                <execution>
                  <id>make-assemble</id>
                  <phase>package</phase>
                  <goals>
                    <goal>single</goal>
                  </goals>
                </execution>
              </executions>
            </plugin>
           </plugins>
        </build>
      
      • 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
      1. 然后执行上面【Maven 打无依赖包】步骤2到步骤4

      第二种方法

      1. 在pom.xml文件中写入

        <plugin>
         <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-jar-plugin</artifactId>
          <version>2.4</version>
          <configuration>
            <archive>
                 <!-- 生成的jar中,不要包含pom.xml和pom.properties这两个文件-->
              <addMavenDescriptor>false</addMavenDescriptor>
              <manifest>
                    <!--是否要把第三方jar放到manifest的classpath中-->
                <addClasspath>true</addClasspath>
                   <!--生成的manifest中classpath的前缀,因为要把第三方jar放到lib目录下,所以classpath的前缀是lib/-->
                <classpathPrefix>lib/</classpathPrefix>
                   <!-- 应用的main class-->
                <mainClass>com.yourClass</mainClass>
              </manifest>
            </archive>
              <!--  过滤掉不希望包含在jar中的文件-->
            <excludes>
              <exclude>${project.basedir}/xml/*</exclude>
            </excludes>
         </configuration>
        </plugin>
        

        <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>2.4</version>
        <configuration>
        <descriptors>
        <descriptor>src/main/assembly/package.xml</descriptor>
        </descriptors>
        </configuration>
        <executions>
        <execution>
        <id>make-assembly</id>
        <phase>package</phase>
        <goals>
        <goal>single</goal>
        </goals>
        </execution>
        </executions>
        </plugin>

        • 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
        • 40
        • 41
        • 42
        • 43
      2. 创建【src/main/assembly】目录

      3. 创建package.xml文件

        <assembly>
          <id>bin</id>
          <!-- 最终打包成一个用于发布的zip文件 -->
          <formats>
              <format>zip</format>
          </formats>
          <!-- Adds dependencies to zip package under lib directory -->
          <dependencySets>
              <dependencySet>
                  <!--
                     不使用项目的artifact,第三方jar不要解压,打包进zip文件的lib目录
                 -->
                  <useProjectArtifact>false</useProjectArtifact>
                  <outputDirectory>lib</outputDirectory>
                  <unpack>false</unpack>
              </dependencySet>
          </dependencySets>
        

        <fileSets>
        <!-- 把项目相关的说明文件,打包进zip文件的根目录 -->
        <fileSet>
        <directory>${project.basedir}</directory>
        <outputDirectory>/</outputDirectory>
        <includes>
        <include>README</include>
        <include>LICENSE
        </include>
        <include>NOTICE*</include>
        </includes>
        </fileSet>

          &lt;!-- 把项目的配置文件,打包进zip文件的config目录 --&gt;
          &lt;fileSet&gt;
              &lt;directory&gt;${project.basedir}/src/main/config&lt;/directory&gt;
              &lt;outputDirectory&gt;config&lt;/outputDirectory&gt;
              &lt;includes&gt;
                  &lt;include&gt;*.xml&lt;/include&gt;
                  &lt;include&gt;*.properties&lt;/include&gt;
              &lt;/includes&gt;
          &lt;/fileSet&gt;
        
          &lt;!-- 把项目的脚本文件目录( src/main/scripts )中的启动脚本文件,打包进zip文件的跟目录 --&gt;
          &lt;fileSet&gt;
              &lt;directory&gt;${project.build.scriptSourceDirectory}&lt;/directory&gt;
              &lt;outputDirectory&gt;&lt;/outputDirectory&gt;
              &lt;includes&gt;
                  &lt;include&gt;startup.*&lt;/include&gt;
              &lt;/includes&gt;
          &lt;/fileSet&gt;
        
          &lt;!-- 把项目的脚本文件(除了启动脚本文件),打包进zip文件的script目录 --&gt;
          &lt;fileSet&gt;
              &lt;directory&gt;${project.build.scriptSourceDirectory}&lt;/directory&gt;
              &lt;outputDirectory&gt;&lt;/outputDirectory&gt;
        
              &lt;excludes&gt;
                  &lt;exclude&gt;startup.*&lt;/exclude&gt;
              &lt;/excludes&gt;
          &lt;/fileSet&gt;
        
          &lt;!-- 把项目自己编译出来的jar文件,打包进zip文件的根目录 --&gt;
          &lt;fileSet&gt;
              &lt;directory&gt;${project.build.directory}&lt;/directory&gt;
              &lt;outputDirectory&gt;&lt;/outputDirectory&gt;
              &lt;includes&gt;
                  &lt;include&gt;*.jar&lt;/include&gt;
              &lt;/includes&gt;
          &lt;/fileSet&gt;
        

        </fileSets>
        </assembly>

        • 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
        • 40
        • 41
        • 42
        • 43
        • 44
        • 45
        • 46
        • 47
        • 48
        • 49
        • 50
        • 51
        • 52
        • 53
        • 54
        • 55
        • 56
        • 57
        • 58
        • 59
        • 60
        • 61
        • 62
        • 63
        • 64
        • 65
        • 66
        • 67
        • 68
        • 69
      4. 将【package.xml】文件放入【assembly】文件夹中
        这里写图片描述

      5. 然后执行上面【Maven 打无依赖包】步骤2到步骤4

如果要打包scala代码需要添加插件:
方案一:

 <plugin>
     <groupId>net.alchim31.maven</groupId>
     <artifactId>scala-maven-plugin</artifactId>
     <version>3.4.0</version>
     <executions>
         <execution>
             <goals>
                 <goal>compile</goal>
                 <goal>testCompile</goal>
             </goals>
         </execution>
     </executions>
 </plugin>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

方案二:

	<plugin>
		<groupId>org.scala-tools</groupId>
		<artifactId>maven-scala-plugin</artifactId>
 		<version>2.15.2</version>
 		<executions>
         	<execution>
            	 <goals>
                 	<goal>compile</goal>
                 	<goal>testCompile</goal>
            	 </goals>
         	</execution>
     	</executions>
	</plugin>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

推荐方案一,方案二中的插件最新更新日期是2011年

简单粗暴
将下面的任意一种方案放到pom文件中,并修改成自己项目中的MainClass即可

</plugins>
    </build>
     方案一 or 方案二
     <build>
<plugins>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 方案一:

    org.apache.maven.plugins maven-shade-plugin 3.0.0 package shade com.test.MainClass
  • 方案二:

    org.apache.maven.plugins maven-assembly-plugin 2.4 com.test.MainClass jar-with-dependencies
    <executions>
        <execution>
            <id>make-assembly</id>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
        </execution>
    </executions>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

注:

本文属于作者原创,如需转载,请注明。

内部如果引用的文字,连接,图片等资源存在侵犯原作者的情况,请联系本人,立即删除。

标签:xml,maven,gt,jar,IDEA,Maven,lt
From: https://www.cnblogs.com/sunny3158/p/17361683.html

相关文章

  • 将IDEA MAVEN项目打包成jar包的通用方法
    将IDEAMAVEN项目打包成jar包的通用方法原文链接:https://blog.csdn.net/qq_56964962/article/details/1255240021.找到左上角File->ProjectStructure,点击(中文:文件->项目结构)2.在弹出的界面中,点击Artifacts(中文:工件),点击内置页面的左上角的+号,选中第一个的第二个,点击 ......
  • java jar 没有主清单属性_Spring Boot jar中没有主清单属性的解决方法「建议收藏」
    javajar没有主清单属性_SpringBootjar中没有主清单属性的解决方法「建议收藏」原文链接:https://cloud.tencent.com/developer/article/2133065大家好,又见面了,我是你们的朋友全栈君。使用SpringBoot微服务搭建框架,在eclipse和Idea下能正常运行,但是在打成jar包部署或者直接......
  • 全网最全Linux 运行jar包的几种方式
    全网最全Linux运行jar包的几种方式原文链接:https://blog.csdn.net/zhangmingyue1226/article/details/127831360一、Linux运行jar包的几种方式方式一:java-jarxxx.jar最常用的启动jar包命令,特点:当前ssh窗口被锁定,可按CTRL+C打断程序运行,或直接关闭窗口,程序退出。方式......
  • idea快捷键
    Ctrl+Shift+M:移动至大括号Ctrl+Shift+U:大小写转换Ctrl+Shift+C:复制文件路径Ctrl+Q:快速文档Ctrl+E:最近的文件Ctrl+Alt+Space:第二次代码补全Ctrl+P:参数信息提醒Ctrl+Alt+Shift+J:选择所有出现的位置Ctrl+Shift+Up:向上移动语句Ctrl+Shift......
  • jar包启动基础命令 java -jar common.jar cmd 命令行执行jar包
    jar包启动基础命令命令行执行jar包java-jarcommon.jar原文链接:https://blog.csdn.net/weixin_40483369/article/details/124392170一般情况下运行jar包,当前是可运行的jar包,直接命令java-jarcommon.jar1按下ctrl+C,关闭当前ssh或者直接关闭窗口,当前程序都会退出。......
  • linux下利用nohup后台运行jar文件包程序
    Linux运行jar包命令如下:方式一: 1.java-jarXXX.jar特点:当前ssh窗口被锁定,可按CTRL+C打断程序运行,或直接关闭窗口,程序退出那如何让窗口不锁定?方式二 1.java-jarXXX.jar&&代表在后台运行。特定:当前ssh窗口不被锁定,但是当窗口关闭时,程序中止运行。继续改......
  • Eclipse没有创建Maven项目入库的原因
    我首先参考了Eclipse历史版本下载和选择对应的java版本这篇文章,发现Eclipse2020-06的版本以后就不支持JDK8了。我想要下载Oxygen版本,但是没有找到入口,然后就准备下载2020-03版本,但是官网的下载速度很慢。所以我就去清华大学开源软件镜像站下载了eclipse-SDK-4.28M......
  • struts 1.2 struts连接池河dbcp连接池所要用的3个jar包
    连接池struts-config.xml里配置<struts-config>下<data-sources> <data-sourcekey="mysqlDB"type="org.apache.commons.dbcp.BasicDataSource"> <set-propertyproperty="driverClassName" val......
  • Maven的Mirror和Repository 的详细讲解
    1Repository(仓库) 1.1Maven仓库主要有2种:remote repository:相当于公共的仓库,大家都能访问到,一般可以用URL的形式访问localrepository:存放在本地磁盘的一个文件夹,例如,windows上默认是C:\Users\{用户名}\.m2\repository目录1.2Remote Repository主要有3种:中央仓库:http://repo1.ma......
  • Maven中mirrors和repository的关系
    1.pom.xml里的repositories元素,里面可以包含多少repository(至少默认包含了中央仓库,该仓库总是在effective-pom里repositories元素的最后一个子元素),每个repository都有一个id(此id非常重要)。2.maven获取真正起作用的repository集合流程:首先会获取pom.xml里的repository......