Maven项目打包jar到私服

快来打我* 2022-06-16 01:44 309阅读 0赞

工具:IntelliJ IDEA、Maven
前提 已经有私服地址(这里假设为 http://192.168.1.112:8081/nexus/),并且maven已经做好相关的配置
maven中settings.xml的配置如下:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
  3. <localRepository>D:\repo</localRepository>
  4. <mirrors>
  5. <mirror>
  6. <id>MyCentral</id>
  7. <name>nexus</name>
  8. <url>http://192.168.1.112:8081/nexus/content/repositories/central/</url>
  9. <mirrorOf>central</mirrorOf>
  10. </mirror>
  11. </mirrors>
  12. <profiles>
  13. <profile>
  14. <id>MyCentral</id>
  15. <repositories>
  16. <repository>
  17. <id>Mythirdparty</id>
  18. <name>SmartCentury thirdparty repository</name>
  19. <url>http://192.168.1.112:8081/nexus/content/repositories/thirdparty/</url>
  20. </repository>
  21. <repository>
  22. <id>My-Central</id>
  23. <name>SmartCentury central repository</name>
  24. <url>http://192.168.1.112:8081/nexus/content/repositories/central/</url>
  25. <releases><enabled>true</enabled></releases>
  26. <snapshots><enabled>true</enabled></snapshots>
  27. </repository>
  28. <repository>
  29. <id>nexus-releases</id>
  30. <name>nexus-releases</name>
  31. <url>http://192.168.1.112:8081/nexus/content/repositories/releases/</url>
  32. </repository>
  33. <repository>
  34. <id>nexus-snapshot</id>
  35. <name>nexus-snapshot</name>
  36. <url>http://192.168.1.112:8081/nexus/content/repositories/snapshots/</url>
  37. </repository>
  38. </repositories>
  39. <pluginRepositories>
  40. <pluginRepository>
  41. <id>MyPlugin</id>
  42. <name>Nexus</name>
  43. <url>>http://192.168.1.112:8081/nexus/content/repositories/central/</url>
  44. <releases><enabled>true</enabled></releases>
  45. <snapshots><enabled>true</enabled></snapshots>
  46. </pluginRepository>
  47. </pluginRepositories>
  48. </profile>
  49. </profiles>
  50. <activeProfiles>
  51. <activeProfile>MyCentral</activeProfile>
  52. </activeProfiles>
  53. </settings>

1、在Maven中settings.xml配置文件中的servers节点中配置如下:

  1. <servers>
  2. <server>
  3. <id>nexus-snapshot</id>
  4. <username>admin</username>
  5. <password>admin123</password>
  6. </server>
  7. <server>
  8. <id>nexus-release</id>
  9. <username>admin</username>
  10. <password>admin123</password>
  11. </server>
  12. </servers>

2、在要打包到私服的maven项目的pom.xml文件中,添加如下配置:

  1. <build>
  2. <plugins>
  3. <plugin>
  4. <artifactId>maven-source-plugin</artifactId>
  5. <version>2.2.1</version>
  6. <executions>
  7. <execution>
  8. <id>attach-sources</id>
  9. <phase>verify</phase>
  10. <goals>
  11. <goal>jar-no-fork</goal>
  12. </goals>
  13. </execution>
  14. </executions>
  15. </plugin>
  16. </plugins>
  17. </build>
  18. <distributionManagement>
  19. <repository>
  20. <id>nexus-releases</id>
  21. <name>Packaging Release Repository</name>
  22. <url>http://192.168.1.209:8081/nexus/content/repositories/releases/</url>
  23. </repository>
  24. <snapshotRepository>
  25. <id>nexus-snapshot</id>
  26. <name>Packaging Snapshot Repository</name>
  27. <url>http://192.168.1.209:8081/nexus/content/repositories/snapshots/</url>
  28. </snapshotRepository>
  29. </distributionManagement>

注:这里的id与settings文件中server的id相对应;maven-source-plugin是为了将源代码可以打包打私服上。

3、如果没有maven,则全部要在pom.xml中配置。还要配置相关的maven plugin

  1. <properties>
  2. <file_encoding>UTF-8</file_encoding>
  3. <project.build.sourceEncoding>${file_encoding}</project.build.sourceEncoding>
  4. <java_source_version>1.7</java_source_version>
  5. <java_target_version>1.7</java_target_version>
  6. <!-- maven插件 -->
  7. <maven_clean_plugin_version>2.5</maven_clean_plugin_version>
  8. <maven_resources_plugin_version>2.6</maven_resources_plugin_version>
  9. <maven_compiler_plugin_version>3.1</maven_compiler_plugin_version>
  10. <maven_source_plugin_version>3.0.1</maven_source_plugin_version>
  11. <maven_jar_plugin_version>2.3.2</maven_jar_plugin_version>
  12. <maven_war_plugin_version>2.1.1</maven_war_plugin_version>
  13. <maven_install_plugin_version>2.5.1</maven_install_plugin_version>
  14. <maven_deploy_plugin_version>2.7</maven_deploy_plugin_version>
  15. <maven_surefire_plugin_version>2.17</maven_surefire_plugin_version>
  16. </properties>
  17. <build>
  18. <pluginManagement>
  19. <plugins>
  20. <plugin>
  21. <groupId>org.apache.maven.plugins</groupId>
  22. <artifactId>maven-clean-plugin</artifactId>
  23. <version>${maven_clean_plugin_version}</version>
  24. </plugin>
  25. <plugin>
  26. <groupId>org.apache.maven.plugins</groupId>
  27. <artifactId>maven-resources-plugin</artifactId>
  28. <version>${maven_resources_plugin_version}</version>
  29. <configuration>
  30. <encoding>${file_encoding}</encoding>
  31. </configuration>
  32. </plugin>
  33. <plugin>
  34. <groupId>org.apache.maven.plugins</groupId>
  35. <artifactId>maven-compiler-plugin</artifactId>
  36. <version>${maven_compiler_plugin_version}</version>
  37. <configuration>
  38. <fork>true</fork>
  39. <source>${java_source_version}</source>
  40. <target>${java_target_version}</target>
  41. <encoding>${file_encoding}</encoding>
  42. </configuration>
  43. </plugin>
  44. <plugin>
  45. <groupId>org.apache.maven.plugins</groupId>
  46. <artifactId>maven-source-plugin</artifactId>
  47. <version>${maven_source_plugin_version}</version>
  48. <executions>
  49. <execution>
  50. <id>attach-sources</id>
  51. <phase>package</phase>
  52. <goals>
  53. <goal>jar-no-fork</goal>
  54. </goals>
  55. </execution>
  56. </executions>
  57. </plugin>
  58. <plugin>
  59. <groupId>org.apache.maven.plugins</groupId>
  60. <artifactId>maven-jar-plugin</artifactId>
  61. <version>${maven_jar_plugin_version}</version>
  62. <configuration>
  63. <archive>
  64. <addMavenDescriptor>true</addMavenDescriptor>
  65. <index>true</index>
  66. <manifest>
  67. <addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
  68. <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
  69. </manifest>
  70. </archive>
  71. </configuration>
  72. </plugin>
  73. <plugin>
  74. <groupId>org.apache.maven.plugins</groupId>
  75. <artifactId>maven-war-plugin</artifactId>
  76. <version>${maven_war_plugin_version}</version>
  77. </plugin>
  78. <plugin>
  79. <groupId>org.apache.maven.plugins</groupId>
  80. <artifactId>maven-install-plugin</artifactId>
  81. <version>${maven_install_plugin_version}</version>
  82. </plugin>
  83. <plugin>
  84. <groupId>org.apache.maven.plugins</groupId>
  85. <artifactId>maven-deploy-plugin</artifactId>
  86. <version>${maven_deploy_plugin_version}</version>
  87. </plugin>
  88. <plugin>
  89. <groupId>org.apache.maven.plugins</groupId>
  90. <artifactId>maven-surefire-plugin</artifactId>
  91. <version>${maven_surefire_plugin_version}</version>
  92. </plugin>
  93. </plugins>
  94. </pluginManagement>
  95. <plugins>
  96. <plugin>
  97. <groupId>org.apache.maven.plugins</groupId>
  98. <artifactId>maven-deploy-plugin</artifactId>
  99. <configuration>
  100. <skip>false</skip>
  101. </configuration>
  102. </plugin>
  103. </plugins>
  104. </build>
  105. <repositories>
  106. <repository>
  107. <id>nexus-releases</id>
  108. <name>nexus-releases</name>
  109. <url>http://192.168.1.112:8081/nexus/content/repositories/releases/</url>
  110. <releases><enabled>true</enabled></releases>
  111. </repository>
  112. <repository>
  113. <id>nexus-snapshot</id>
  114. <name>nexus-snapshot</name>
  115. <url>http://192.168.1.112:8081/nexus/content/repositories/snapshots/</url>
  116. <snapshots><enabled>true</enabled></snapshots>
  117. </repository>
  118. </repositories>
  119. <!-- 部署到nexus私服上的配置 -->
  120. <distributionManagement>
  121. <repository>
  122. <id>nexus-releases</id>
  123. <name>nexus-releases</name>
  124. <url>http://192.168.1.112:8081/nexus/content/repositories/releases/</url>
  125. </repository>
  126. <snapshotRepository>
  127. <id>nexus-snapshot</id>
  128. <name>nexus-snapshot</name>
  129. <url>http://192.168.1.112:8081/nexus/content/repositories/snapshots/</url>
  130. </snapshotRepository>
  131. </distributionManagement>

4、输入 mvn deploy 命令,即可将jar包和source文件一并上传到私服上。

发表评论

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

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

相关阅读