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

相關文章
相關標籤/搜索