【Maven】打包时推送源码至 maven 仓库

432 阅读1分钟
  • 在父pom中增加

【提示】
如果没有子模块,直接在工程的 pom.xml 中添加 maven-source-plugin 插件就可以

<pluginManagement>
   <plugins>
      <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-source-plugin</artifactId>
          <version>3.0.1</version>
          <configuration>
             <attach>true</attach>
          </configuration>
             <executions>
                 <execution>
                     <phase>compile</phase>
                         <goals>
                            <goal>jar</goal>
                         </goals>
                 </execution>
            </executions>
        </plugin>
    </plugins>
</pluginManagement>
  • 子项目中增加
<build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-source-plugin</artifactId>
      </plugin>
    </plugins>
  </build>
  • 然后使用:mvn deploy 或 mvn install

  • 查看mvn日志, 源码确实被打包进仓库

...省略...
[INFO] Building jar: E:\newWorld\javaTest\target\javaTest-1.0-SNAPSHOT.jar
[INFO] 
[INFO] --- install:3.1.1:install (default-install) @ javaTest ---
[INFO] Installing E:\newWorld\javaTest\target\javaTest-1.0-SNAPSHOT.jar to D:\Users\.m2\repository\fun\javaTest\1.0-SNAPSHOT\javaTest-1.0-SNAPSHOT.jar
[INFO] Installing E:\newWorld\javaTest\target\consumer10874856746976604338pom to D:\Users\.m2\repository\fun\javaTest\1.0-SNAPSHOT\javaTest-1.0-SNAPSHOT.pom
[INFO] Installing E:\newWorld\javaTest\target\javaTest-1.0-SNAPSHOT-sources.jar to D:\Users\.m2\repository\fun\javaTest\1.0-SNAPSHOT\javaTest-1.0-SNAPSHOT-sources.jar
[INFO] Copying fun:javaTest:pom:1.0-SNAPSHOT to project local repository
[INFO] Copying fun:javaTest:jar:1.0-SNAPSHOT to project local repository
[INFO] Copying fun:javaTest:pom:consumer:1.0-SNAPSHOT to project local repository
[INFO] Copying fun.ferment.neonsea:javaTest:jar:sources:1.0-SNAPSHOT to project local repository
[INFO] --------------------------------------------------------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] --------------------------------------------------------------------------------------------------------------------------
[INFO] Total time:  3.047 s
[INFO] Finished at: 2024-06-12T11:26:35+08:00
[INFO] --------------------------------------------------------------------------------------------------------------------------

...省略...