maven 配置 pom.xml 打包生成:单jar包/jar包+lib目录

系统管理员 2023-01-17 10:57 595阅读 0赞

生成jar包 2种方式:
1.打包项目依赖的所有jar 生成单个jar包
2.生成jar包 复制所需的依赖jar包到lib目录

Maven打包过程(顺序)

  • clean清空之前生成的文件
  • IDE内编译该程序 (并测试可成功运行)— 必须生成class文件!(等待被打包)
  • 确定文件pom.xml中的各项配置
  • Maven刷新:Reimport All Maven Projects
  • Maven自动打包:Install或package

可打包项目已经编译生成的class文件 + 依赖的jar包 —> 生成单个jar包(可独立运行)

例1

下面这个pom.xml,可打包项目已经编译生成的class文件 + 依赖的jar包 —> 生成单个jar包(可独立运行)
searchgeturl-1.0-SNAPSHOT-jar-with-dependencies.jar

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <groupId>groupId</groupId>
  7. <artifactId>searchgeturl</artifactId>
  8. <version>1.0-SNAPSHOT</version>
  9. <build>
  10. <plugins>
  11. <plugin>
  12. <artifactId>maven-assembly-plugin</artifactId>
  13. <configuration>
  14. <archive>
  15. <manifest>
  16. <addClasspath>true</addClasspath>
  17. <!--下面必须指定好主类 如com.my.Main -->
  18. <mainClass>Main</mainClass>
  19. </manifest>
  20. </archive>
  21. <descriptorRefs>
  22. <descriptorRef>jar-with-dependencies</descriptorRef>
  23. </descriptorRefs>
  24. </configuration>
  25. <executions>
  26. <execution>
  27. <id>make-my-jar-with-dependencies</id>
  28. <phase>package</phase>
  29. <goals>
  30. <goal>single</goal>
  31. </goals>
  32. </execution>
  33. </executions>
  34. </plugin>
  35. </plugins>
  36. </build>
  37. <!-- 项目依赖的jar包 -->
  38. <dependencies>
  39. <dependency>
  40. <!-- jsoup HTML parser library @ http://jsoup.org/ -->
  41. <groupId>org.jsoup</groupId>
  42. <artifactId>jsoup</artifactId>
  43. <version>1.10.2</version>
  44. </dependency>
  45. <!-- https://mvnrepository.com/artifact/commons-cli/commons-cli -->
  46. <dependency>
  47. <groupId>commons-cli</groupId>
  48. <artifactId>commons-cli</artifactId>
  49. <version>1.2</version>
  50. </dependency>
  51. </dependencies>
  52. <!-- 指定工程编码为UTF-8
  53. 这样maven install就不会发出警告 [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
  54. -->
  55. <properties>
  56. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  57. </properties>
  58. </project>

例1的pom.xml 得到的 完整目录结构:

  1. .
  2. ├── archive-tmp
  3. ├── classes
  4. ├── CommandDemo.class
  5. ├── META-INF
  6. └── MANIFEST.MF
  7. └── Main.class
  8. ├── maven-archiver
  9. └── pom.properties
  10. ├── maven-status
  11. └── maven-compiler-plugin
  12. ├── compile
  13. └── default-compile
  14. ├── createdFiles.lst
  15. └── inputFiles.lst
  16. └── testCompile
  17. └── default-testCompile
  18. └── inputFiles.lst
  19. ├── searchgeturl-1.0-SNAPSHOT-jar-with-dependencies.jar
  20. └── searchgeturl-1.0-SNAPSHOT.jar

例2

下面这个pom.xml,可打包项目已经编译生成的class文件 + 依赖的jar包 —> 生成单个jar包(可独立运行)
Cknife-1.0-SNAPSHOT-jar-with-dependencies.jar

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <groupId>groupId</groupId>
  7. <artifactId>Cknife</artifactId>
  8. <version>1.0-SNAPSHOT</version>
  9. <build>
  10. <plugins>
  11. <plugin>
  12. <artifactId>maven-assembly-plugin</artifactId>
  13. <configuration>
  14. <archive>
  15. <manifest>
  16. <addClasspath>true</addClasspath>
  17. <!--下面必须指定好主类-->
  18. <mainClass>com.ui.Cknife</mainClass>
  19. </manifest>
  20. </archive>
  21. <descriptorRefs>
  22. <descriptorRef>jar-with-dependencies</descriptorRef>
  23. </descriptorRefs>
  24. </configuration>
  25. <executions>
  26. <execution>
  27. <id>make-my-jar-with-dependencies</id>
  28. <phase>package</phase>
  29. <goals>
  30. <goal>single</goal>
  31. </goals>
  32. </execution>
  33. </executions>
  34. </plugin>
  35. <plugin>
  36. <groupId>org.apache.maven.plugins</groupId>
  37. <artifactId>maven-compiler-plugin</artifactId>
  38. <configuration>
  39. <source>1.7</source><!-- 源代码使用的开发版本 -->
  40. <target>1.8</target><!-- 需要生成的目标class文件的编译版本 -->
  41. <!-- 一般而言,target和source保持一致的,但有时候不同:为了让程序能在其他版本的jdk中运行(对于低版本目标jdk,源代码中需要不使用低版本jdk不支持的语法) -->
  42. </configuration>
  43. </plugin>
  44. </plugins>
  45. </build>
  46. <!-- 项目依赖的jar包 -->
  47. <dependencies>
  48. <!-- https://mvnrepository.com/artifact/org.xerial/sqlite-jdbc -->
  49. <dependency>
  50. <groupId>org.xerial</groupId>
  51. <artifactId>sqlite-jdbc</artifactId>
  52. <version>3.16.1</version>
  53. </dependency>
  54. <!-- https://mvnrepository.com/artifact/com.github.insubstantial/substance -->
  55. <dependency>
  56. <groupId>com.github.insubstantial</groupId>
  57. <artifactId>substance</artifactId>
  58. <version>7.3</version>
  59. </dependency>
  60. <!-- https://mvnrepository.com/artifact/com.github.insubstantial/trident -->
  61. <dependency>
  62. <groupId>com.github.insubstantial</groupId>
  63. <artifactId>trident</artifactId>
  64. <version>7.3</version>
  65. </dependency>
  66. </dependencies>
  67. <!-- 指定工程编码为UTF-8
  68. 这样maven install就不会发出警告 [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
  69. -->
  70. <properties>
  71. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  72. </properties>
  73. </project>


生成:jar包+lib目录(依赖包)

例:下面这个pom.xml 生成 jar包+lib目录(含依赖包)

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <groupId>groupId</groupId>
  7. <artifactId>searchgeturl</artifactId>
  8. <version>1.0-SNAPSHOT</version>
  9. <build>
  10. <plugins>
  11. <!-- 下面这个plugin-->
  12. <plugin>
  13. <groupId>org.apache.maven.plugins</groupId>
  14. <artifactId>maven-jar-plugin</artifactId>
  15. <configuration>
  16. <archive>
  17. <manifest>
  18. <addClasspath>true</addClasspath>
  19. <classpathPrefix>lib/</classpathPrefix><!--指定classpath的前缀-->
  20. <mainClass>Main</mainClass><!--指定主类的类名-->
  21. </manifest>
  22. </archive>
  23. </configuration>
  24. </plugin>
  25. <!-- -->
  26. <plugin>
  27. <groupId>org.apache.maven.plugins</groupId>
  28. <artifactId>maven-dependency-plugin</artifactId>
  29. <executions>
  30. <execution>
  31. <id>copy-dependencies</id>
  32. <phase>prepare-package</phase>
  33. <goals>
  34. <goal>copy-dependencies</goal>
  35. </goals>
  36. <configuration>
  37. <!--指定outputDirectory-->
  38. <outputDirectory>${project.build.directory}/lib</outputDirectory>
  39. <!--
  40. <outputDirectory>${project.build.directory}/classes/lib</outputDirectory>
  41. Alternatively use ${project.build.directory}/classes/lib as OutputDirectory to integrate all jar-files into the main jar, but then you will need to add custom classloading code to load the jars.
  42. -->
  43. <overWriteReleases>false</overWriteReleases>
  44. <overWriteSnapshots>false</overWriteSnapshots>
  45. <overWriteIfNewer>true</overWriteIfNewer>
  46. </configuration>
  47. </execution>
  48. </executions>
  49. </plugin>
  50. </plugins>
  51. </build>
  52. <!-- 项目依赖的jar包 -->
  53. <dependencies>
  54. <dependency>
  55. <!-- jsoup HTML parser library @ http://jsoup.org/ -->
  56. <groupId>org.jsoup</groupId>
  57. <artifactId>jsoup</artifactId>
  58. <version>1.10.2</version>
  59. </dependency>
  60. <!-- https://mvnrepository.com/artifact/commons-cli/commons-cli -->
  61. <dependency>
  62. <groupId>commons-cli</groupId>
  63. <artifactId>commons-cli</artifactId>
  64. <version>1.2</version>
  65. </dependency>
  66. </dependencies>
  67. <!-- 指定工程编码为UTF-8
  68. 这样maven install就不会发出警告 [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
  69. -->
  70. <properties>
  71. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  72. </properties>
  73. </project>
  74. #用下面的pom.xml 得到的 一部分目录结构(保证下面的目录结构即可运行jar程序)
  75. ─── lib #依赖包
  76. │ ├── commons-cli-1.2.jar
  77. │ └── jsoup-1.10.2.jar
  78. └── searchgeturl-1.0-SNAPSHOT.jar #maven生成的 主jar包
  79. #用下面的pom.xml 得到的 完整目录结构:
  80. ── classes #maven生成的
  81. │ ├── CommandDemo.class
  82. │ ├── META-INF
  83. │ │ └── MANIFEST.MF #maven生成的 清淡文件
  84. │ └── Main.class
  85. ├── lib #依赖包
  86. │ ├── commons-cli-1.2.jar
  87. │ └── jsoup-1.10.2.jar
  88. ├── maven-archiver #maven生成的
  89. │ └── pom.properties
  90. ├── maven-status#maven生成的
  91. │ └── maven-compiler-plugin
  92. │ ├── compile
  93. │ │ └── default-compile
  94. │ │ ├── createdFiles.lst
  95. │ │ └── inputFiles.lst
  96. │ └── testCompile
  97. │ └── default-testCompile
  98. │ └── inputFiles.lst
  99. └── searchgeturl-1.0-SNAPSHOT.jar #maven生成的 主jar包

作者:极客圈
链接:https://www.jianshu.com/p/9146cec6cc60
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

发表评论

表情:
评论列表 (有 0 条评论,595人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Android工程打包生成jar

                最近项目要结尾了,最主要的是还要把工程源码给客户,要把工程打包!     该工程包含两个项目,一个项目是类库项目,里面提供了好多方法、组件、效果等等;