项目构建之maven篇:2.HelloWorld项目构建过程

文件结构说明:

项目构建生命周期:

清理

编译

测试

打包

运行

部署

清理与编译

hello\pom.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:版本

hello\src\main\java下的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文件夹的内容

查看本地仓库

测试:

hello\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>
</project>

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

hello\src\test\java\HelloTest.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

查看本地仓库

源代码下载

项目构建之maven篇:2.HelloWorld项目构建过程,布布扣,bubuko.com

时间: 2024-08-01 22:47:25

项目构建之maven篇:2.HelloWorld项目构建过程的相关文章

项目构建之maven篇:6.生命周期与插件

项目生命周期 清理 初始化 编译 测试 打包 部署 三套生命周期 1.clean pre-clean 执行一些需要在clean之前完成的工作 clean 移除所有上一次构建生成的文件 post-clean 执行一些需要在clean之后立刻完成的工作 2.compile validate generate-sources process-sources generate-resources process-resources 复制并处理资源文件,至目标目录,准备打包. compile 编译项目的源

项目构建之maven篇:1.环境搭建

maven下载: 下载地址 设置环境变量 查看安装成功 mvn -version 设置本地仓库 存放从中央仓库或私服中下载依赖包,自定义的项目使用install命令,安装到本地仓库中,供其它项目依赖 apache-maven-3.0.5\conf\settings.xml 项目构建之maven篇:1.环境搭建,布布扣,bubuko.com

项目构建之maven篇:5.仓库及nexus创建私服-1

依赖坐标与本地仓库存储 <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>3.0.7.RELEASE</version> </dependency> 本地仓库存储 仓库分类: 本地仓库: <localRepository>F:\maven\repos<

项目构建之maven篇:8.maven发布web工程及基于spring mvc,jetty实现的用户管理demo

web工程目录结构 pom/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&qu

项目构建之maven篇:3.m2eclipse使用

m2eclipse的安装 略 设置maven目录 设置用户个性化的maven配置 导入maven工程 源代码下载 运行命令 运行订制命令: mvn clean install或 mvn clean compile 可以自己订制 新建maven工程 新建一个Hello.java并运行 install命令 项目构建之maven篇:3.m2eclipse使用,布布扣,bubuko.com

项目构建之maven篇:5.仓库及nexus创建私服-2

下载安装 下载地址 修改默认端口: home\conf\nexus.properties # Sonatype Nexus # ============== # This is the most basic configuration of Nexus. # Jetty section application-port=9080 application-host=0.0.0.0 nexus-webapp=${bundleBasedir}/nexus nexus-webapp-context-pa

项目构建之maven篇:4.坐标与依赖及spring依赖注入demo

源代码下载 坐标 <groupId>com.demo.animal</groupId> <artifactId>animal-core</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> groupId:定义当前maven项目隶属的实际项目 artifactId:定义实际项目中的一个maven项目(模块),推荐实际项

项目构建之maven篇:7.聚合与继承

聚合: 为方便项目构建,通常将聚合模块放在项目目录的最顶层,其他模块则作为聚合模块的子目录存在, 这样我们得到源码的时候,最快发现的是聚合模块的pom 运行命令 查看结果 源代码下载 继承 创建parent项目,用于被继承 parent/pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

3.将maven项目jar纳入maven仓库,Mave项目依赖另外一个Maven项目的案例

 1 若想让maven项目依赖另外一个maven项目,被依赖的项目要在maven仓库中有相应的jar包,所以要对依赖的项目执行mvninstall命令. 2 新建第二个项目模块HelloFriend目录及约定的目录结构 HelloFriend --src -----main ----------java ----------resources -----test ---------java ---------resources --pom.xml 3 在项目HelloFriend根目录建立p