qileilove

blog已经转移至github,大家请访问 http://qaseven.github.io/

maven3实战之maven使用入门(打包和运行)

maven3实战之maven使用入门(打包和运行)

----------

 

将项目进行编译,测试之后,下一个重要步骤就是打包。pom.xml中如果没有指定打包类型,默认打包类型为:jar。我们可以简单地执行命令打包: mvn clean pachage

类似地,maven会在打包之前执行编译,测试等操作。如以打jar包为例,jar:jar任务负责打包,实际上就是jar插件的jar目标将项目主代码打包成一个名为xxxxxx-1.0-SNAPSHOT.jar的文件。该文件也位于target/输出目录中,它是根据artifact-version.jar的规则进行命名的,如有需要,还可以使用finalName来自定义该文件的名称,这里暂且不展开,后面会详细解释。

在打包之后,又执行了安装任务install:install。从输出可以看到任务将项目输出的jar安装到了maven本地仓库中,可以打开相应的文件夹看到项目的pom和jar。之前我们说只有构件被下载到本地仓库后,才能由所有Maven项目使用,这里同样的道理,只有将自己编写的项目安装到本地仓库之后,其他maven项目才能使用它。

我们已经体验了maven最主要的命令:mvn clean compile,mvn clean test,mvn clean package,mvn clean install。执行test之前是会先执行compile的,执行package之前是会先执行test的,而类似地,install之前会执行package。可以在任何一个maven项目中执行这些命令。

默认打包是生成jar的,如果我的项目还有main方法的,我想打包成可执行的jar包,为了生成可执行的jar文件,需要借助maven-shade-plugin,配置该插件如下:

 

 

Xml代码  收藏代码
  1. <plugin>  
  2.     <groupId>org.apache.maven.plugins</groupId>  
  3.     <artifactId>maven-shade-plugin</artifactId>  
  4.     <version>1.2.1</version>  
  5.     <executions>  
  6.         <execution>  
  7.             <phase>package</phase>  
  8.             <goals>  
  9.                 <goal>shade</goal>  
  10.             </goals>  
  11.             <configuration>  
  12.                 <transformers>  
  13.                     <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">  
  14.                         <mainClass>com.juvenxu.mvnbook.helloworld.HelloWorld</mainClass>  
  15.                     </transformer>  
  16.                 </transformers>  
  17.             </configuration>  
  18.         </execution>  
  19.     </executions>  
  20. </plugin>  

 

 

默认打包生成jar是不能够直接运行的,因为带有main方法的类信息不会添加到manifest中(打开jar文件中的META-INF/MANIFEST.MF文件,将无法看到Main-Class一行)。

plugin元素在POM中的相对位置应该在<project><build><plugins>下面。我们配置了mainClass为com.juvenxu.mvnbook.helloworld.HelloWorld,项目在打包时会将该信息放到MANIFEST中。现在执行mvn clean install,待构建完成之后打开target/目录,可以看到hello-world-1.0-SNAPSHOT.jar和original-hello-world-1.0-SNAPSHOT.jar,前者是带有Main-Class信息的可执行jar,后者是原始的jar,打开hello-world-1.0-SNAPSHOT.jar的META-INF/MANIFEST.MF,可以看到它包含这样一行信息:

Main-Class:com.juvenxu.mvnbook.helloworld.HelloWorld

现在,在项目根目录中执行该jar文件:

D:\code\hello-world > java-jar target\hello-world-1.0-SNAPSHOT.jar,可以得到正确的输出了。

1:创建名为:helloword的maven项目

2:/helloword/src/main/java下建立package包hello,在包下建类SayHello

package hello;

public class SayHello {

    public String sayhello() {
        System.out.println("hello............is anybody here...");
        return "dddddddddddddddddddddddddd";
    }

    public static void main(String[] arg) {
        System.out.println(new SayHello().sayhello());
    }
}
3:/helloword/src/test/java下建立test包,在test包下建HelloWordTest类

package test;

import hello.SayHello;
import junit.framework.Assert;
import junit.framework.TestCase;

public class HelloWordTest extends TestCase {

    public HelloWordTest(String name) {
        super(name);
    }
      protected void setUp() {                                         //进行初始化的任务
            
          }
    /**
     * @param args
     */
    public void testSayHello()
    {
        SayHello s = new SayHello();
        String r = s.sayhello();
        Assert.assertEquals("dddddddddddddddddddddddddd", r);
    }
}

4:pom.xml配置

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.houwen.test</groupId>
  <artifactId>helloword</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>Maven Helloword</name>

<!--junit依赖是为tset准备,配置后,记得install,把依赖包嵌入resourse内,-->
   <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    </dependencies>
        <build>

<!--maven-shade-plugin

而要得到一个可以直接在命令行通过java命令运行的JAR文件,还要满足两个条件:
  JAR包中的/META-INF/MANIFEST.MF元数据文件必须包含Main-Class信息。

Maven有好几个插件能帮助用户完成上述任务,不过用起来最方便的还是maven-shade-plugin,它可以让用户配置Main-Class的值,然后在打包的时候将值填入/META-INF/MANIFEST.MF文件。关于项目的依赖,它很聪明地将依赖JAR文件全部解压后,再将得到的.class文件连同当前项目的.class文件一起合并到最终的CLI包中,这样,在执行CLI JAR文件的时候,所有需要的类就都在Classpath中了。下面是一个配置样例:

-->
        <plugins>
        <plugin>
           <groupId>org.apache.maven.plugins</groupId>
           <artifactId>maven-shade-plugin</artifactId>
           <version>1.2.1</version>
           <executions>
                      <execution>
               <phase>package</phase>
               <goals>
                     <goal>shade</goal>
               </goals>
               <configuration>
                     <transformers>
                       <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                        <mainClass>hello.SayHello</mainClass>
                      </transformer>
                     </transformers>
               </configuration>
                </execution>
           </executions>
        </plugin>
        </plugins>
    </build>
</project>


4.1上述例子中的,运行maven package后

我的Main-Class是hello.SayHello,构建完成后,对应于一个常规的helloword-0.0.1-SNAPSHOT.jar文件,我还得到了一个original-helloword-0.0.1-SNAPSHOT.jar文件。最后,我可以通过helloword-0.0.1-SNAPSHOT.jar命令运行程序。

4.2之后再运行maven install,把生成的jar打入到本地的maven仓库,可以在C:\Documents and Settings\hcen\.m2\repository\com\houwen\test\helloword\0.0.1-SNAPSHOT

找到helloword-0.0.1-SNAPSHOT.jar

5.最后,我可以通过java -jar  (jar包路径)/helloword-0.0.1-SNAPSHOT.jar命令运行程序。

用Maven生成一个包孕所有依赖jar包的可执行的jar包


create an executable jar with dependencies using Maven:

using plugin - onejar-maven-plugin. Example below (mvn package build jar):

<plugin>
     <groupId>org.dstovall</groupId>
     <artifactId>onejar-maven-plugin</artifactId>
     <version>1.3.0</version>
     <executions>
           <execution>
              <configuration>
                  <mainClass>com.company.MainClass</mainClass>
              </configuration>
              <goals>
                  <goal>one-jar</goal>
              </goals>
           </execution>
     </executions>
</plugin>


You need to add repository for that plugin:

<pluginRepositories>
     <pluginRepository>
           <id>onejar-maven-plugin.googlecode.com</id>
           <url>http://onejar-maven-plugin.googlecode.com/svn/mavenrepo</url>
     </pluginRepository>
</pluginRepositories>


posted on 2014-03-11 17:30 顺其自然EVO 阅读(100488) 评论(0)  编辑  收藏 所属分类: maven


只有注册用户登录后才能发表评论。


网站导航:
 
<2024年4月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

导航

统计

常用链接

留言簿(55)

随笔分类

随笔档案

文章分类

文章档案

搜索

最新评论

阅读排行榜

评论排行榜