maven打jar例子

来源:互联网 发布:网络言论自由的案例 编辑:程序博客网 时间:2024/05/09 21:40

:创建名为: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命令运行程序。
0 0
原创粉丝点击