• 编写基本的java程序,输出jar,maven打包jar


    Java学习之:如何将 java 程序打包成 .jar 文件_暖仔会飞的博客-CSDN博客_java文件打包成jar

    Javaweb项目导出成jar包并使用Windows定时任务定时执行 - 信铁寒胜 - 博客园

    Java jar打包成exe应用程序 - 掘金

    Maven 各种花式构建,不用 SpringBoot 也能打出可执行 Jar 包 - 掘金

    mvn打jar包示例:依赖打入jar包和依赖打到外部文件夹_一首简单的歌-shining的博客-CSDN博客


     

    =================mvn打jar包示例:依赖打入jar包和依赖打到外部文件夹

    使用maven打包java的jar包时,通常有两种情况:

    将依赖打到外部文件夹,将源码单独打jar包运行;

    将依赖和源码一起打到jar包中运行。

    下面举例说明这两种情况:

    建立如下测试类,依赖一个common-lang包(用于测试外部依赖):

    1. package mvnDemo;
    2. import org.apache.commons.lang3.StringUtils;
    3. public class MvnDemo {
    4. public static void main(String[] args) {
    5. System.out.println(StringUtils.upperCase("hello mvn"));
    6. }
    7. }

    POM配置如下:

    1. <project xmlns="http://maven.apache.org/POM/4.0.0"
    2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    4. <modelVersion>4.0.0modelVersion>
    5. <groupId>org.examplegroupId>
    6. <artifactId>testDemoartifactId>
    7. <version>1.0-SNAPSHOTversion>
    8. <dependencies>
    9. <dependency>
    10. <groupId>org.apache.commonsgroupId>
    11. <artifactId>commons-lang3artifactId>
    12. <version>3.11version>
    13. dependency>
    14. <dependency>
    15. <groupId>junitgroupId>
    16. <artifactId>junitartifactId>
    17. <version>RELEASEversion>
    18. <scope>compilescope>
    19. dependency>
    20. dependencies>
    21. <build>
    22. <plugins>
    23. <plugin>
    24. <groupId>org.apache.maven.pluginsgroupId>
    25. <artifactId>maven-compiler-pluginartifactId>
    26. <configuration>
    27. <source>8source>
    28. <target>8target>
    29. configuration>
    30. plugin>
    31. <plugin>
    32. <groupId>org.apache.maven.pluginsgroupId>
    33. <artifactId>maven-jar-pluginartifactId>
    34. <version>3.2.0version>
    35. <configuration>
    36. <archive>
    37. <manifest>
    38. <mainClass>
    39. mvnDemo.MvnDemo
    40. mainClass>
    41. <addClasspath>
    42. true
    43. addClasspath>
    44. <classpathPrefix>
    45. lib
    46. classpathPrefix>
    47. manifest>
    48. archive>
    49. configuration>
    50. plugin>
    51. <plugin>
    52. <groupId>org.apache.maven.pluginsgroupId>
    53. <artifactId>maven-dependency-pluginartifactId>
    54. <version>3.2.0version>
    55. <executions>
    56. <execution>
    57. <id>copy-dependenciesid>
    58. <phase>packagephase>
    59. <goals>
    60. <goal>copy-dependenciesgoal>
    61. goals>
    62. <configuration>
    63. <outputDirectory>${project.build.directory}/liboutputDirectory>
    64. configuration>
    65. execution>
    66. executions>
    67. plugin>
    68. <plugin>
    69. <groupId>org.apache.maven.pluginsgroupId>
    70. <artifactId>maven-shade-pluginartifactId>
    71. <version>3.2.4version>
    72. <executions>
    73. <execution>
    74. <phase>packagephase>
    75. <goals>
    76. <goal>shadegoal>
    77. goals>
    78. <configuration>
    79. <filters>
    80. <filter>
    81. <artifact>*:*artifact>
    82. <excludes>
    83. <exclude>META-INF/*.SFexclude>
    84. <exclude>META-INF/*.DSAexclude>
    85. <exclude>META-INF/*.RSAexclude>
    86. excludes>
    87. filter>
    88. filters>
    89. <transformers>
    90. <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
    91. <mainClass>mvnDemo.MvnDemomainClass>
    92. transformer>
    93. transformers>
    94. configuration>
    95. execution>
    96. executions>
    97. plugin>
    98. plugins>
    99. build>
    100. project>

    打完的jar如下:

    lib中存放的是相关依赖jar包(比如common-lang包);

    original-testDemo-1.0-SNAPSHOT.jar是仅含源码的jar包;

    testDemo-1.0-SNAPSHOT.jar是既包含源码又包含相关依赖的jar包。

    上述两个jar包分别使用java -jar original-testDemo-1.0-SNAPSHOT.jar  java -jar testDemo-1.0-SNAPSHOT.jar运行都能得到

    HELLO MVN

    的运行结果。
    ————————————————

    =====maven 打包剥离配置文件和外部依赖jar 到指定文件目录

    1. <properties>
    2. <output.dependence.file.path>lib/output.dependence.file.path>
    3. <output.resource.file.path>resource/output.resource.file.path>
    4. <output.jar.name>wen-toolsoutput.jar.name>
    5. <output.jar.main>com.wen.Mainoutput.jar.main>
    6. properties>
    7. <build>
    8. <finalName>${output.jar.name}finalName>
    9. <plugins>
    10. <plugin>
    11. <groupId>org.apache.maven.pluginsgroupId>
    12. <artifactId>maven-jar-pluginartifactId>
    13. <configuration>
    14. <excludes>
    15. <exclude>*.propertiesexclude>
    16. <exclude>*.ymlexclude>
    17. <exclude>*/*.propertiesexclude>
    18. <exclude>*/*.ymlexclude>
    19. excludes>
    20. <archive>
    21. <manifest>
    22. <addClasspath>trueaddClasspath>
    23. <classpathPrefix>${output.dependence.file.path}classpathPrefix>
    24. <useUniqueVersions>falseuseUniqueVersions>
    25. <mainClass>${output.jar.main}mainClass>
    26. manifest>
    27. <manifestEntries>
    28. <Class-Path>./${output.resource.file.path}Class-Path>
    29. manifestEntries>
    30. archive>
    31. <outputDirectory>${project.build.directory}outputDirectory>
    32. configuration>
    33. plugin>
    34. <plugin>
    35. <groupId>org.apache.maven.pluginsgroupId>
    36. <artifactId>maven-dependency-pluginartifactId>
    37. <executions>
    38. <execution>
    39. <id>copy-dependenciesid>
    40. <phase>packagephase>
    41. <goals>
    42. <goal>copy-dependenciesgoal>
    43. goals>
    44. <configuration>
    45. <outputDirectory>${project.build.directory}/${output.dependence.file.path}outputDirectory>
    46. configuration>
    47. execution>
    48. executions>
    49. plugin>
    50. <plugin>
    51. <artifactId>maven-resources-pluginartifactId>
    52. <executions>
    53. <execution>
    54. <id>copy-resourcesid>
    55. <phase>packagephase>
    56. <goals>
    57. <goal>copy-resourcesgoal>
    58. goals>
    59. <configuration>
    60. <resources>
    61. <resource>
    62. <directory>src/main/resourcesdirectory>
    63. <includes>
    64. <include>*.propertiesinclude>
    65. <include>*.ymlinclude>
    66. <include>*/*.propertiesinclude>
    67. <include>*/*.propertiesinclude>
    68. includes>
    69. resource>
    70. resources>
    71. <outputDirectory>${project.build.directory}/${output.resource.file.path}outputDirectory>
    72. configuration>
    73. execution>
    74. executions>
    75. plugin>
    76. <plugin>
    77. <groupId>org.springframework.bootgroupId>
    78. <artifactId>spring-boot-maven-pluginartifactId>
    79. <configuration>
    80. <includes>
    81. <include>
    82. <groupId>nullgroupId>
    83. <artifactId>nullartifactId>
    84. include>
    85. includes>
    86. <outputDirectory>${project.build.directory}outputDirectory>
    87. configuration>
    88. <executions>
    89. <execution>
    90. <goals>
    91. <goal>repackagegoal>
    92. goals>
    93. <configuration>
    94. configuration>
    95. execution>
    96. executions>
    97. plugin>
    98. <plugin>
    99. <groupId>org.apache.maven.pluginsgroupId>
    100. <artifactId>maven-surefire-pluginartifactId>
    101. <configuration>
    102. <skip>trueskip>
    103. configuration>
    104. plugin>
    105. plugins>
    106. build>

    ----------------------------------------

    Java——maven打完jar包之后将jar包放到指定位置总结_前方一片光明的博客-CSDN博客_maven编译后的jar包放在哪里
     

    Maven打包到指定位置_AllenLeungX的博客-CSDN博客_maven打包指定文件目录

     

  • 相关阅读:
    Kafka - 3.x 副本不完全指北
    【无标题】shell_43.Linux三种在 shell 脚本中处理选项的方法
    vue引入路由——帮助管理页面跳转逻辑
    面试官:介绍一下 Redis 三种集群模式
    java毕业设计班费收支管理系统mybatis+源码+调试部署+系统+数据库+lw
    《SQLi-Labs》04. Less 23~28a
    【JavaSE语法】数据类型与变量
    力扣刷题记录167.1-----1356. 根据数字二进制下 1 的数目排序
    MySQL遵循最左前缀匹配原则!面试官:回去等通知吧
    centos7快速搭建ftp服务器
  • 原文地址:https://blog.csdn.net/qq_18932003/article/details/126022980