Maven

创建maven项目

mvn archetype:generate


mvn clean 清除target下编译内容

mvn compile 编译项目

mvn test 运行测试

mvn install 打包并放到本地Maven仓库

mvn install -Dmaven.test.skip=ture 跳过测试

mvn deploy 上传jar包(需配置Maven安装目录下的conf/settings.xml)

mvn package 打包发布

mvn package -Dmaven.test.skip=ture 跳过测试

mvn clean package -e -U

-e: 详细异常 -U: 强制更新依赖


生成依赖树

mvn dependency:tree -Ddetail=true


生成清除eclipse项目结构

mvn eclipse:eclipse

mvn eclipse:clean


帮助

mvn help:help -Ddetail=true



Maven生命周期

Maven有三套相互独立的生命周期,分别是clean、default和site。每个生命周期包含一些阶段(phase),阶段是有顺序的,后面的阶段依赖于前面的阶段。


1、clean生命周期:清理项目,包含三个phase。
1)pre-clean:执行清理前需要完成的工作
2)clean:清理上一次构建生成的文件
3)post-clean:执行清理后需要完成的工作


2、default生命周期:构建项目,重要的phase如下。
1)validate:验证工程是否正确,所有需要的资源是否可用。
2)compile:编译项目的源代码。  
3)test:使用合适的单元测试框架来测试已编译的源代码。这些测试不需要已打包和布署。
4)Package:把已编译的代码打包成可发布的格式,比如jar。
5)integration-test:如有需要,将包处理和发布到一个能够进行集成测试的环境。
6)verify:运行所有检查,验证包是否有效且达到质量标准。
7)install:把包安装到maven本地仓库,可以被其他工程作为依赖来使用。
8)Deploy:在集成或者发布环境下执行,将最终版本的包拷贝到远程的repository,使得其他的开发者或者工程可以共享。


3、site生命周期:建立和发布项目站点,phase如下
1)pre-site:生成项目站点之前需要完成的工作
2)site:生成项目站点文档
3)post-site:生成项目站点之后需要完成的工作
4)site-deploy:将项目站点发布到服务器



MAVEN插件

Jar

<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-jar-plugin</artifactId>
	<configuration>
		<archive>
			<manifest>
				<mainClass>com.shhxzq.stock.t2t.application.Provider</mainClass>
				<addClasspath>true</addClasspath>
				<!--依赖的第三方包路径-->
				<classpathPrefix></classpathPrefix>
			</manifest>
			<manifestEntries>
				<Class-Path>.</Class-Path>
			</manifestEntries>
		</archive>
	</configuration>
</plugin>


Assembly

<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-assembly-plugin</artifactId>
	<version>2.6</version>
	<configuration>
		<descriptors>
			<descriptor>src/main/resources/assembly.xml</descriptor>
		</descriptors>
	</configuration>
	<executions>
		<execution>
			<id>make-assembly</id>
			<phase>package</phase>
			<goals>
				<goal>single</goal>
			</goals>
		</execution>
	</executions>
</plugin>


Assembly.xml

<assembly
	xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
	<id>bin</id>
	<formats>
		<format>zip</format>
	</formats>
	<includeBaseDirectory>true</includeBaseDirectory>
	<dependencySets>
		<dependencySet>
			<useProjectArtifact>true</useProjectArtifact>
			<outputDirectory>/lib</outputDirectory>
			<unpack>false</unpack>
			<scope>runtime</scope>
		</dependencySet>
	</dependencySets>
	<fileSets>
		<fileSet>
			<outputDirectory>/</outputDirectory>
			<includes>
				<include>README.txt</include>
			</includes>
		</fileSet>
		<fileSet>
			<directory>src/main/scripts</directory>
			<outputDirectory>/</outputDirectory>
			<includes>
				<include>start.sh</include>
			</includes>
		</fileSet>
	</fileSets>
</assembly>


Shade

<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-shade-plugin</artifactId>
	<version>2.4.3</version>
	<executions>
		<execution>
			<id>shade-all</id>
			<phase>package</phase>
			<goals>
				<goal>shade</goal>
			</goals>
			<configuration>
			<outputFile>target/shade/${project.artifactId}-${project.version}.jar</outputFile>
				<createSourcesJar>true</createSourcesJar>
				<createDependencyReducedPom>false</createDependencyReducedPom>
				<promoteTransitiveDependencies>true</promoteTransitiveDependencies>
				<artifactSet>
					<includes>
						<include>**</include>
					</includes>
				</artifactSet>
				<transformers>
					<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
						<mainClass>com.shhxzq.stock.t2t.application.Provider</mainClass>
					</transformer>
					<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
						<resource>META-INF/spring.handlers</resource>
					</transformer>
					<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
						<resource>META-INF/spring.schemas</resource>
					</transformer>
				</transformers>
			</configuration>
		</execution>
	</executions>
</plugin>


resource

<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-resources-plugin</artifactId>
	<version>2.6</version>
	<executions>
		<execution>
			<id>copy-resources</id>
			<!-- 在default生命周期的 validate阶段就执行resources插件的copy-resources目标 -->
			<phase>validate</phase>
			<!-- <phase>compile</phase> -->
			<goals>
				<goal>copy-resources</goal>
			</goals>
			<configuration>
				<!-- 指定resources插件处理资源文件到哪个目录下 -->
				<outputDirectory>${project.build.outputDirectory}</outputDirectory>
				<!--  也可以用下面这样的方式(指定相对url的方式指定outputDirectory)
				<outputDirectory>target/classes</outputDirectory>
				-->
				<!-- 待处理的资源定义 -->
				<resources>
					<resource>
						<!-- 指定resources插件处理哪个目录下的资源文件 -->
						<directory>src/main/resources</directory>
						<!-- 指定不需要处理的资源 
						<excludes>
							<exclude>WEB-INF/*.*</exclude>
						</excludes>
						-->
						<!-- 是否对待处理的资源开启过滤模式 (resources插件的copy-resources目标也有资源过滤的功能,这里配置的
							这个功能的效果跟<build><resources><resource>下配置的资源过滤是一样的,只不过可能执行的阶段不一样,
							这里执行的阶段是插件指定的{phase}阶段,<build><resources><resource>下的配置将是在resources插件
							的resources目标执行时起作用(在process-resources阶段))-->
						<filtering>true</filtering>
					</resource>
				</resources>
			</configuration>
			<inherited></inherited>
		</execution>
	</executions>
</plugin>


surefire (配置Junit测试

<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-surefire-plugin</artifactId>
	<version>2.4.2</version>
	<configuration>
		<skipTests>true</skipTests>
		<includes>
			<include>**/*ServiceTest.java</include>
		</includes>
		<excludes>
			<exclude>**/TempTest.java</exclude>
			<exclude>**/*DaoTest.java</exclude>
		</excludes>
	</configuration>
</plugin>


Maven多环境配置

使用filter过滤resource,通过profile切换环境

<filters>
	<filter>../conf/filters/env-${env}.properties</filter>
	<!-- <filter>${user.home}/zqjy.properties</filter> -->
</filters>

<resources>
	<resource>
		<directory>src/main/resources</directory>
		<!-- 是否使用过滤器 -->
		<filtering>true</filtering>
	</resource>
	<resource>
		<directory>${basedir}/src/main/java</directory>
		<includes>
			<include>**/*.xml</include>
		</includes>
	</resource>
</resources>

<profiles>
	<profile>
		<id>dev</id>
		<properties>
			<env>dev</env>
		</properties>
		<activation>
			<activeByDefault>true</activeByDefault>
		</activation>
	</profile>
	<profile>
		<id>test</id>
		<properties>
			<env>test</env>
		</properties>
	</profile>
	<profile>
		<id>product</id>
		<properties>
			<env>product</env>
		</properties>
	</profile>
</profiles>

Maven内置属性

http://docs.codehaus.org/display/MAVENUSER/MavenPropertiesGuide


有六种类型的Maven属性:


内置属性:主要有两个常用内置属性——${basedir}表示项目根目录,即包含pom.xml文件的目录;${version}表示项目版本。


POM属性:pom中对应元素的值。例如${project.artifactId}对应了<project><artifactId>元素的值,常用的POM属性包括:

${project.build.sourceDirectory}:项目的主源码目录,默认为src/main/java/.

${project.build.testSourceDirectory}:项目的测试源码目录,默认为/src/test/java/.

${project.build.directory}:项目构建输出目录,默认为target/.

${project.outputDirectory}:项目主代码编译输出目录,默认为target/classes/.

${project.testOutputDirectory}:项目测试代码编译输出目录,默认为target/testclasses/.

${project.groupId}:项目的groupId.${project.artifactId}:项目的artifactId.

${project.version}:项目的version,于${version}等价

${project.build.finalName}:项目打包输出文件的名称,默认为${project.artifactId}${project.version}.


自定义属性:在pom中<properties>元素下自定义的Maven属性。例如

<project>
	<properties>
		<my.prop>hello</my.prop>
	</properties>
</project>


Settings属性:与POM属性同理。如${settings.localRepository}指向用户本地仓库的地址。


Java系统属性:所有Java系统属性都可以使用Maven属性引用,例如${user.home}指向了用户目录。可以通过命令行mvn help:system查看所有的Java系统属性


环境变量属性:所有环境变量都可以使用以env.开头的Maven属性引用。例如${env.JAVA_HOME}指代了JAVA_HOME环境变量的值。也可以通过命令行mvn help:system查看所有环境变量。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值