首页 > 技术文章 > 【maven详解-插件】maven插件学习之maven-source-plugin

yantz 2015-04-28 16:58 原文

在pom.xml文件build节点下加入以下代码:

 1 <plugin>  
 2         <groupId>org.apache.maven.plugins</groupId>  
 3         <artifactId>maven-source-plugin</artifactId>  
 4         <version>2.1.1</version>  
 5         <executions>  
 6             <execution>  
 7                 <id>attach-sources</id>  
 8                 <phase>package</phase>
 9                 <goals>  
10                     <goal>jar-no-fork</goal>  
11                 </goals>  
12             </execution>  
13         </executions>  
14 </plugin> 

用户可以根据需要将任何插件目标绑定到任何生命周期的阶段,如:将maven-source-plugin的jar-no-fork目标绑定到default生命周期的package阶段,这样,以后在执行mvn package命令打包项目时,在package阶段之后会执行源代码打包

execution下可配置phase属性, 意思是在什么阶段打包源文件。如<phase>install</phase>:在执行mvn install时打包源代码

DOS执行mvn install生成source.jar

 1 [INFO] ------------------------------------------------------------------------
 2 [INFO] Building common-base 0.0.1
 3 [INFO] ------------------------------------------------------------------------
 4 Downloading: http://121.41.105.153:8081/nexus/content/groups/public/org/apache/m
 5 aven/plugins/maven-source-plugin/maven-metadata.xml
 6 Downloaded: http://121.41.105.153:8081/nexus/content/groups/public/org/apache/ma
 7 ven/plugins/maven-source-plugin/maven-metadata.xml (746 B at 5.6 KB/sec)
 8 Downloading: http://121.41.105.153:8081/nexus/content/groups/public/org/codehaus
 9 /mojo/cobertura-maven-plugin/maven-metadata.xml
10 Downloaded: http://121.41.105.153:8081/nexus/content/groups/public/org/codehaus/
11 mojo/cobertura-maven-plugin/maven-metadata.xml (605 B at 4.7 KB/sec)
12 Downloading: http://121.41.105.153:8081/nexus/content/groups/public/org/apache/m
13 aven/plugins/maven-help-plugin/maven-metadata.xml
14 Downloaded: http://121.41.105.153:8081/nexus/content/groups/public/org/apache/ma
15 ven/plugins/maven-help-plugin/maven-metadata.xml (493 B at 3.7 KB/sec)
16 [INFO]
17 [INFO] --- maven-resources-plugin:2.4.3:resources (default-resources) @ common-b
18 ase ---
19 [INFO] Using 'UTF-8' encoding to copy filtered resources.
20 [INFO] Copying 6 resources
21 [INFO]
22 [INFO] --- maven-compiler-plugin:2.0.2:compile (default-compile) @ common-base -
23 --
24 [INFO] Compiling 180 source files to E:\cncrowd_workspace\common-deploy\common-b
25 ase\target\classes
26 [INFO]
27 [INFO] --- maven-resources-plugin:2.4.3:testResources (default-testResources) @
28 common-base ---
29 [INFO] Using 'UTF-8' encoding to copy filtered resources.
30 [INFO] Copying 2 resources
31 [INFO]
32 [INFO] --- maven-compiler-plugin:2.0.2:testCompile (default-testCompile) @ commo
33 n-base ---
34 [INFO] Nothing to compile - all classes are up to date
35 [INFO]
36 [INFO] --- maven-surefire-plugin:2.7.2:test (default-test) @ common-base ---
37 [INFO] Surefire report directory: E:\cncrowd_workspace\common-deploy\common-base
38 \target\surefire-reports
39 There are no tests to run.
40 [INFO]
41 [INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ common-base ---
42 [INFO] Building jar: E:\cncrowd_workspace\common-deploy\common-base\target\commo
43 n-base-0.0.1.jar
44 [INFO]
45 [INFO] >>> maven-source-plugin:2.4:jar (attach-sources) @ common-base >>>
46 [INFO]
47 [INFO] <<< maven-source-plugin:2.4:jar (attach-sources) @ common-base <<<
48 [INFO]
49 [INFO] --- maven-source-plugin:2.4:jar (attach-sources) @ common-base ---
50 [INFO] Building jar: E:\cncrowd_workspace\common-deploy\common-base\target\commo
51 n-base-0.0.1-sources.jar
52 [INFO]
53 [INFO] --- maven-install-plugin:2.5.1:install (default-install) @ common-base --
54 -
55 [INFO] Installing E:\cncrowd_workspace\common-deploy\common-base\target\common-b
56 ase-0.0.1.jar to D:\Maven\repository\com\common\item\base\common-base\0.0.1\comm
57 on-base-0.0.1.jar
58 [INFO] Installing E:\cncrowd_workspace\common-deploy\common-base\pom.xml to D:\M
59 aven\repository\com\common\item\base\common-base\0.0.1\common-base-0.0.1.pom
60 [INFO] Installing E:\cncrowd_workspace\common-deploy\common-base\target\common-b
61 ase-0.0.1-sources.jar to D:\Maven\repository\com\common\item\base\common-base\0.
62 0.1\common-base-0.0.1-sources.jar

执行结果:

补充:

执行 mvn install,maven会自动将source install到repository 。

执行 mvn deploy,maven会自动将source deploy到remote-repository 。

执行 mvn source:jar,单独打包源码。

手动安装MAVEN安装source代码到本地库:

mvn install:install-file -Dfile=E:/common-base-0.0.1-sources.jar -DgroupId=net.spy -DartifactId=spymemcached -Dversion=2.10.3 -Dpackaging=jar -Dclassifier=sources

 

推荐阅读