Maven快速入门教程

一 环境配置

配置settings.xml

打开Eclipse->Preferences->Maven->User Settings,设置User Settings的用户配置文件,点击Update Settings使文件生效。

settings.xml

<?xml version="1.0" encoding="UTF-8"?>

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
	<servers>
	    <server>
		<id>releases</id>
		<username>deployment</username>
		<password>deployment123</password>
		</server>
	</servers>
	<mirrors>
		<mirror>
			<id>nexus-local</id>
			<mirrorOf>central</mirrorOf>
			<name>Nexus osc</name>
			<url>http://192.168.1.118:8081/nexus/content/groups/public/</url>
		</mirror>
	</mirrors>
	<profiles>
		<profile>
			<id>jdk-1.6</id>
			<activation>
				<jdk>1.6</jdk>
			</activation>
			<properties>         
                <maven.compiler.source>1.6</maven.compiler.source>         
                <maven.compiler.target>1.6</maven.compiler.target>         
                <maven.compiler.compilerVersion>1.6</maven.compiler.compilerVersion>         
            </properties>
			<repositories>
				<repository>
					<id>nexus</id>
					<name>local private nexus</name>
					<url>http://192.168.1.118:8081/nexus/content/groups/public/</url>
					<releases>
						<enabled>true</enabled>
					</releases>
					<snapshots>
						<enabled>false</enabled>
					</snapshots>
				</repository>
			</repositories>
			<pluginRepositories>
				<pluginRepository>
					<id>nexus</id>
					<name>local private nexus</name>
					<url>http://192.168.1.118:8081/nexus/content/groups/public/</url>
					<releases>
						<enabled>true</enabled>
					</releases>
					<snapshots>
						<enabled>false</enabled>
					</snapshots>
				</pluginRepository>
			</pluginRepositories>
		</profile>
	</profiles>
</settings>

二 使用

1.新建项目

1).新建SE项目

点击File->New->Other,选择Maven Project。在Select Archetype中选择maven-archetype-quickstart。输入Group Id, Artifact Id等项目信息后点击Finish.

2).新建Web项目

点击File->New->Other,选择Maven Project。在Select Archetype中选择maven-archetype-webapp。输入Group Id, Artifact Id等项目信息后点击Finish。

然后在项目根目录中新建src/main/java与src/test/java两个文件夹。

3).将其他项目转为Maven项目

右击项目名称,选择Configure->Convert To Maven Project


注意:

如果项目需要用到配置文件,需要新建名为src/main/resources的Source Folder,并将输出路径设为target/classes.配置文件作用域为classpath。

2.添加依赖jar文件

方法一:

在浏览器中打开http://192.168.1.118:8081/nexus,搜索要添加的包名称,在搜索结果中点击目标jar文件,将右下角XML框中的内容复制到项目pom.xml中的<dependencies>标签下,然后右击项目名称->Maven->Update Project。

方法二:

右击项目名称->Maven->Add Dependency,输入要添加的jar包关键字进行搜索,选择好jar文件后点击OK即可。

注:依赖的scope标签规定了依赖jar文件的作用范围,

作用范围
test 单元测试(打包时不编译)
provided 由运行时环境提供(不编译)
compile(默认值) 编译进执行文件

3.运行Maven命令

命令运行方式:右击项目名称Run As->Maven Build,在goals中输入要运行的命令,可以输入多个命令(用空格分开)。

常用命令:

clean: 清理项目

compile: 编译项目

package: 打包项目

install: 将项目打包(Jar)并安装到本地Maven仓库

deploy: 将项目jar文件上传到Maven仓库(私服)

三 部署

1.部署web项目

运行package命令,在项目根目录下的target目录下生成${finalName}.war

2.打包可执行jar文件

修改pom.xml文件,在<project>标签中添加如下代码

<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-assembly-plugin</artifactId>
				<version>2.2</version>
				<configuration>
					<descriptorRefs>
						<descriptorRef>jar-with-dependencies</descriptorRef>
					</descriptorRefs>
					<archive>
						<manifest>
							<mainClass>可执行jar文件的入口类</mainClass>
						</manifest>
					</archive>
				</configuration>
				<executions>
					<execution>
						<id>make-assembly</id> <!-- this is used for inheritance merges -->
						<phase>package</phase> <!-- bind to the packaging phase -->
						<goals>
							<goal>single</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>

运行package命令即可在项目target目录中生成包含依赖jar包的可执行文件 ${项目名-版本号}-with-dependencies.jar

3.生成源码jar(src.jar)文件

修改pom.xml文件,在<project>标签中添加如下代码

<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-source-plugin</artifactId>
				<version>2.4</version>
				<executions>
					<execution>
						<id>attach-sources</id>
						<phase>package</phase>
						<goals>
							<goal>jar</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>

运行package命令即可在项目target目录中生成 ${项目名-版本号}-sources.jar

你可能感兴趣的:(Maven快速入门教程)