maven生成可执行jar

元素名称     简 介
<project>     POM的xml根元素
<parent>     声明继承
<modules>     声明聚合
<groupId>     坐标元素之一
project -root element of pom.xml files.
modelVersion -设置POM version, 相似于设置project version,目前好像maven 2只容许该值为4.0.0.
groupId -设置project的organization or group 的ID,它是project的惟一识别之一(另1个是artifactId)。groupId一般以full domain name做为属性值,例如全部maven plugins的groupId的属性值为org.apache.maven.plugins.
artifactId -设置当前project将生成的primary artifact的unique base name(请注意:是base name,而不是name,由于artifact name是由”<artifactId>-<version>”组成,例如myapp-1.0.jar).
packaging 坐标元素之一,默认值jar-设置package type to be used by this artifact (e.g. JAR, WAR, EAR, etc.)。该属性值不只是用来设置被生成的artifact是JAR, WAR仍是EAR,还用来设置package artifact时要process的lifecycle,即JAR, WAR, EAR的build lifecycle是不一样的。packaging的缺省属性值为JAR。若是你的project要package成jar,你不须要设置该属性.
version -设置project生成的artifact的version。Maven能帮你进行version management。若是你看到该属性值里包含有“SNAPSHOT”string,则表示该project处于开发阶段,而不是发布阶段.
name -设置project name,该属性值一般用于Maven's generated documentation.
url -设置该project的web site url. 该属性值一般用于Maven's generated documentation.
description -设置project description该属性值一般用于Maven's generated documentation.

1.新建一个组paohaijiao.pockbox.maven ,在这个组上的id为helloworld
2.新建一个类package paohaijiao.pockbox.maven.helloworld;

package paohaijiao.pockbox.maven.helloworld;

public class Helloworld {
    public String sayHello(){
        return "Hello Maven";
    }

    public static void main(String[] args) {
        System.out.println(new Helloworld().sayHello());

    }

}

3.在pom上运行 clean compile
clean 清除target下的目录
compile 编译文件到D:\project\helloworld\target\classes
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building helloworld 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.4.1:clean (default-clean) @ helloworld ---
[INFO]
[INFO] --- maven-resources-plugin:2.5:resources (default-resources) @ helloworld ---
[debug] execute contextualize
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory D:\project\helloworld\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ helloworld ---
[INFO] Compiling 1 source file to D:\project\helloworld\target\classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.330s
[INFO] Finished at: Sun Feb 28 13:13:32 CST 2016
[INFO] Final Memory: 5M/10M
[INFO] ------------------------------------------------------------------------
4.在pom文件上添加坐标,maven将会访问中央仓库http://repol.maven.org/maven2去下载junit,默认范围是compile即主代码和测试代码都有效,
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>##主代码无效
    </dependency>
  </dependencies>
  5编写测试文件
  package paohaijiao.pockbox.maven.helloworld;

import static org.junit.Assert.assertEquals;

import org.junit.Test;

public class HelloWorldTest {
    @Test
    public void testSayHello() {
        Helloworld helloworld = new Helloworld();
        String result=helloworld.sayHello();
        assertEquals("Hello Maven",result);
    }

}
6运行clean test

[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building helloworld 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.4.1:clean (default-clean) @ helloworld ---
[INFO] Deleting D:\project\helloworld\target
[INFO]
[INFO] --- maven-resources-plugin:2.5:resources (default-resources) @ helloworld ---
[debug] execute contextualize
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory D:\project\helloworld\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ helloworld ---
[INFO] Compiling 1 source file to D:\project\helloworld\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.5:testResources (default-testResources) @ helloworld ---
[debug] execute contextualize
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory D:\project\helloworld\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:testCompile (default-testCompile) @ helloworld ---
[INFO] Compiling 1 source file to D:\project\helloworld\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.10:test (default-test) @ helloworld ---
[INFO] Surefire report directory: D:\project\helloworld\target\surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running paohaijiao.pockbox.maven.helloworld.HelloWorldTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.05 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.695s
[INFO] Finished at: Sun Feb 28 13:34:06 CST 2016
[INFO] Final Memory: 8M/15M
[INFO] ------------------------------------------------------------------------
7打包运行clean package
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building helloworld 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.4.1:clean (default-clean) @ helloworld ---
[INFO] Deleting D:\project\helloworld\target
[INFO]
[INFO] --- maven-resources-plugin:2.5:resources (default-resources) @ helloworld ---
[debug] execute contextualize
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory D:\project\helloworld\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ helloworld ---
[INFO] Compiling 1 source file to D:\project\helloworld\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.5:testResources (default-testResources) @ helloworld ---
[debug] execute contextualize
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory D:\project\helloworld\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:testCompile (default-testCompile) @ helloworld ---
[INFO] Compiling 1 source file to D:\project\helloworld\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.10:test (default-test) @ helloworld ---
[INFO] Surefire report directory: D:\project\helloworld\target\surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running paohaijiao.pockbox.maven.helloworld.HelloWorldTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.037 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO]
[INFO] --- maven-jar-plugin:2.3.2:jar (default-jar) @ helloworld ---
[INFO] Building jar: D:\project\helloworld\target\helloworld-0.0.1-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.874s
[INFO] Finished at: Sun Feb 28 13:39:42 CST 2016
[INFO] Final Memory: 8M/15M
[INFO] ------------------------------------------------------------------------

8.clean install让其余项目可以直接使用helloworld 这个jar包
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building helloworld 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.4.1:clean (default-clean) @ helloworld ---
[INFO] Deleting D:\project\helloworld\target
[INFO]
[INFO] --- maven-resources-plugin:2.5:resources (default-resources) @ helloworld ---
[debug] execute contextualize
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory D:\project\helloworld\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ helloworld ---
[INFO] Compiling 1 source file to D:\project\helloworld\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.5:testResources (default-testResources) @ helloworld ---
[debug] execute contextualize
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory D:\project\helloworld\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:testCompile (default-testCompile) @ helloworld ---
[INFO] Compiling 1 source file to D:\project\helloworld\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.10:test (default-test) @ helloworld ---
[INFO] Surefire report directory: D:\project\helloworld\target\surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running paohaijiao.pockbox.maven.helloworld.HelloWorldTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.037 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO]
[INFO] --- maven-jar-plugin:2.3.2:jar (default-jar) @ helloworld ---
[INFO] Building jar: D:\project\helloworld\target\helloworld-0.0.1-SNAPSHOT.jar
[INFO]
[INFO] --- maven-install-plugin:2.3.1:install (default-install) @ helloworld ---
[INFO] Installing D:\project\helloworld\target\helloworld-0.0.1-SNAPSHOT.jar to C:\Users\goudcheng\.m2\repository\paohaijiao\pockbox\maven\helloworld\0.0.1-SNAPSHOT\helloworld-0.0.1-SNAPSHOT.jar
[INFO] Installing D:\project\helloworld\pom.xml to C:\Users\goudcheng\.m2\repository\paohaijiao\pockbox\maven\helloworld\0.0.1-SNAPSHOT\helloworld-0.0.1-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.325s
[INFO] Finished at: Sun Feb 28 13:41:18 CST 2016
[INFO] Final Memory: 8M/16M
[INFO] ------------------------------------------------------------------------
9生成可执行jar
 <build>  
    <plugins>  
      <plugin>  
        <groupId>org.apache.maven.plugins</groupId>  
        <artifactId>maven-shade-plugin</artifactId>   
        <version>1.2.1</version>  
        <configuration>  
          <transformers>  
         <transformer implementation = "org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">  
         <mainClass>paohaijiao.pockbox.maven.helloworld.HelloWorld</mainClass>  
         </transformer>  
      </transformers>  
        </configuration>  
        <executions>  
          <execution>  
            <phase>package</phase>  
            <goals>  
              <goal>shade</goal>  
            </goals>  
          </execution>  
        </executions>  
      </plugin>  
      
    </plugins>  
    
  </build>  

 

web

相关文章
相关标签/搜索