zoukankan      html  css  js  c++  java
  • 项目构建之maven篇:2.HelloWorld项目构建过程

    文件结构说明:





    项目构建生命周期:


    清理

    编译

    測试

    打包

    执行

    部署


    清理与编译


    hellopom.xml


    POM:Project Object Model,项目对象模型

    pom.xml与ant的build.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.demo.hello</groupId>
    	<artifactId>hello-world</artifactId>
    	<version>1.0.0-SNAPSHOT</version>	
    	<name>hello</name>
    </project>

    说明:


    modelVersion:指定当前POM模型的版本号,Maven2及Maven3仅仅能是4.0.0

    groupId:项目组名称

    artifactId:当前Maven项目在组中的唯一的id

    version:版本号


    hellosrcmainjava下的Hello.java

    package com.demo ;
    public class Hello {
    	public void sayHi(){
    		System.out.println("hello world");
    	}
    	public static void main(String [] args){
    		new Hello().sayHi();
    	}
    }

    执行清理及编译命令:


    进入hello的目录路径,执行

    mvn clean compile

    执行结果





    查看target目录的内容






    查看本地仓库






    測试:


    hellopom.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.demo.hello</groupId>
    	<artifactId>hello-world</artifactId>
    	<version>1.0.0-SNAPSHOT</version>	
    	<name>hello</name>
    	<properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      </properties>
    
    	  <dependencies>
    		<dependency>
    		  <groupId>junit</groupId>
    		  <artifactId>junit</artifactId>
    		  <version>4.10</version>
    		  <scope>test</scope>
    		</dependency>
    	  </dependencies>
    </project>

    dependencies:指明这个项目所须要的依赖包

    hellosrc estjavaHelloTest.java

    package com.demo;
    
    import org.junit.Test;
    
    
    public class HelloTest {
    	@Test
    	public void testHello(){
    		new Hello().sayHi();	
    	}
    }
    

    执行測试命令

    mvn clean test


    查看结果





    查看本地仓库





    打包

    执行命令


    mvn clean package


    查看结果






    执行


    又一次改造pom.xml,增加插件

    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.demo.hello</groupId>
    	<artifactId>hello-world</artifactId>
    	<version>1.0.0-SNAPSHOT</version>	
    	<name>hello</name>
    	<properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      </properties>
    
    	  <dependencies>
    		<dependency>
    		  <groupId>junit</groupId>
    		  <artifactId>junit</artifactId>
    		  <version>4.10</version>
    		  <scope>test</scope>
    		</dependency>
    	  </dependencies>
    
    	  <build>
        <plugins>
      	  <plugin>
      	    <groupId>org.apache.maven.plugins</groupId>
      	    <artifactId>maven-compiler-plugin</artifactId>
      	    <configuration>
      	      <source>1.5</source>
      	      <target>1.5</target>
      	    </configuration>
      	  </plugin>
      	  <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>com.demo.Hello</mainClass>
      	            </transformer>
      	          </transformers>
      	        </configuration>
      	      </execution>
      	    </executions>
      	  </plugin>
        </plugins>
      </build>
    </project>


    又一次执行打包命令:


    mvn clean package


    执行完成后,进入hello/target下,执行命令


    java -jar hello-world-1.0.0-SNAPSHOT.jar



    查看结果:





    安装到本地仓库,供其他项目依赖


    执行命令


    mvn clean install



    查看本地仓库





    源码下载


  • 相关阅读:
    冲印数码照片的奥秘
    买手机(一)
    情人节的考验
    一日三茶
    追寻童年的乐趣在PSP上玩童年玩过的街机游戏
    转贴:常用的美国俚语
    用左手画CAD
    桃花朵朵开春游南汇桃花节
    不用GPRS照样用手机上网以后手机费不能报销的朋友们也能用手机上网啦!
    双休日去爬山
  • 原文地址:https://www.cnblogs.com/mengfanrong/p/5139830.html
Copyright © 2011-2022 走看看