Maven打成可执行jar包的配置

「爱情、让人受尽委屈。」 2022-05-12 09:36 341阅读 0赞

maven打可执行jar包

本文主要参考:

http://blog.csdn.net/strongyoung88/article/details/54097830

首先需要知道如何建立一个maven项目:

mvn archetype:generate -DarchetypeCatalog=internal

然后修改pom文件

  1. 4.0.0
  1. <groupId>com.xueyoucto.xueyou</groupId>
  2. <artifactId>MyFirstMavenProject</artifactId>
  3. <version>1.0-SNAPSHOT</version>
  4. <packaging>jar</packaging>
  5. <name>MyFirstMavenProject</name>
  6. <url>http://maven.apache.org</url>
  7. <properties>
  8. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  9. </properties>
  10. <dependencies>
  11. <dependency>
  12. <groupId>junit</groupId>
  13. <artifactId>junit</artifactId>
  14. <version>3.8.1</version>
  15. <scope>test</scope>
  16. </dependency>
  17. <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
  18. <dependency>
  19. <groupId>org.apache.commons</groupId>
  20. <artifactId>commons-lang3</artifactId>
  21. <version>3.4</version>
  22. </dependency>
  23. </dependencies>
  24. <build>
  25. <plugins>
  26. <plugin>
  27. <groupId>org.apache.maven.plugins</groupId>
  28. <artifactId>maven-compiler-plugin</artifactId>
  29. <configuration>
  30. <source>1.8</source>
  31. <target>1.8</target>
  32. <encoding>utf-8</encoding>
  33. </configuration>
  34. </plugin>
  35. <plugin>
  36. <artifactId>maven-assembly-plugin</artifactId>
  37. <version>3.0.0</version>
  38. <configuration>
  39. <archive>
  40. <manifest>
  41. <mainClass>com.xueyoucto.xueyou.App</mainClass>
  42. </manifest>
  43. </archive>
  44. <descriptorRefs>
  45. <descriptorRef>jar-with-dependencies</descriptorRef>
  46. </descriptorRefs>
  47. </configuration>
  48. <executions>
  49. <execution>
  50. <id>make-assembly</id> <!-- this is used for inheritance merges -->
  51. <phase>package</phase> <!-- bind to the packaging phase -->
  52. <goals>
  53. <goal>single</goal>
  54. </goals>
  55. </execution>
  56. </executions>
  57. </plugin>
  58. </plugins>
  59. </build>
  60. </project>
  61. 其中:


  1. org.apache.maven.plugins

    maven-compiler-plugin



    1.8

    1.8

    utf-8



这段是负责编译的时候使用jdk的版本和编码格式。

  1. <plugin>
  2. <artifactId>maven-assembly-plugin</artifactId>
  3. <version>3.0.0</version>
  4. <configuration>
  5. <archive>
  6. <manifest>
  7. <mainClass>com.xueyoucto.xueyou.App</mainClass>
  8. </manifest>
  9. </archive>
  10. <descriptorRefs>
  11. <descriptorRef>jar-with-dependencies</descriptorRef>
  12. </descriptorRefs>
  13. </configuration>
  14. <executions>
  15. <execution>
  16. <id>make-assembly</id> <!-- this is used for inheritance merges -->
  17. <phase>package</phase> <!-- bind to the packaging phase -->
  18. <goals>
  19. <goal>single</goal>
  20. </goals>
  21. </execution>
  22. </executions>
  23. </plugin>

这段有两个功能,一个是设置Main函数,一个是在jar包中引入依赖的jar包。

下面是使用mvn package后生成的jar包。

Center

运行这个jar包:

Center 1

发表评论

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

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

相关阅读

    相关 Idea执行jar

    前些日子试了下idea打包,有些细节没太注意所以经常打包失败,要不然就是显示没有主清单属性,所以一直用eclipse打包,今天又重新捣鼓了一下,写下过程: 1. 先添加需要