Maven使用说明

一、常用命令

命令解释
mvn clean清理target目录
mvn package项目打包,在target目录下生成jar或war文件
mvn install将jar或war文件复制到本地仓库
mvn deploy将打包的文件发布到远程仓库,可以配置到私服
以上命令可以组合使用,如mvn clean package会先清理target目录,然后再打包。

二、使用说明

1、配置私服

①Artifactory
1)单项目配置,修改项目pom.xml文件,增加以下配置

    <repositories>
        <repository>
            <id>artifactory</id>
            <name>your local artifactory</name>
            <url>http://localhost:8081/artifactory/repo</url>
        </repository>
    </repositories>

    <pluginRepositories>
        <pluginRepository>
            <id>artifactory</id>
            <name>your local artifactory</name>
            <url>http://localhost:8081/artifactory/plugins-releases</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>

2)全局配置

②Nexus
1)单项目配置

2)全局配置,修改MAVEN_HOME/conf/setting.xml
增加profile节点,将activeProfile节点的值设置为新增节点的id。

<profiles>   
<profile>  
  <id>profile-nexus</id>  
  
  <repositories>  
<repository>  
  <id>nexus</id>  
  <url>http://192.168.1.204:8081/nexus/content/groups/public/</url>  
  <snapshots><enabled>true</enabled></snapshots>  
  <releases><enabled>true</enabled></releases>  
</repository>  
  </repositories>  
  <pluginRepositories>  
<pluginRepository>  
  <id>nexus</id>  
  <url>http://192.168.1.204:8081/nexus/content/groups/public/</url>  
  <snapshots><enabled>true</enabled></snapshots>  
  <releases><enabled>true</enabled></releases>  
</pluginRepository>  
  </pluginRepositories>  
</profile> 
</profiles>
  <activeProfiles>
	<activeProfile>profile-nexus</activeProfile>
  </activeProfiles> 
3、将本地构建发布到私服

修改MAVEN_HOME/conf/setting,增加server节点,输入id名称。用户名密码对应的是私服的登录用户名密码。

<servers>
<server>  
  <id>nexus-snapshots</id>  
  <username>admin</username>  
  <password>admin123</password>  
</server>  
<server>  
  <id>nexus-releases</id>  
  <username>admin</username>  
  <password>admin123</password>  
</server>  

<server>  
  <id>artifactory-snapshots</id>  
  <username>admin</username>  
  <password>p@ssw0rd</password>  
</server>  
<server>  
  <id>artifactory-releases</id>  
  <username>admin</username>  
  <password>p@ssw0rd</password>  
</server>  
</servers>

①Artifactory
修改pom.xml文件,增加以下内容,其中snapshotRepository节点和repository节点的id分别指向上一步骤中server节点对应的id。maven_test为仓库名称

<!--配置将本地构建提交到nexus私服-->
<distributionManagement>
		<!-- 配置快照版本发布的仓库-->
		<snapshotRepository>
			<id>artifactory-snapshots</id>
			<name>Nexus Snapshots Repository</name>
			<url>http://192.168.1.204:9081/artifactory/maven_test/</url>
		</snapshotRepository>
		<!-- 配置release版本发布的仓库-->
		<repository>
			<id>artifactory-releases</id>
			<name>Nexus Releases Repository</name>
			<url>http://192.168.1.204:9081/artifactory/maven_test/</url>
		</repository>
</distributionManagement>

②Nexus
修改项目pom.xml文件,增加以下内容,其中snapshotRepository节点和repository节点的id分别指向上一步骤中server节点对应的id。

<!--配置将本地构建提交到nexus私服-->
<distributionManagement>
<!-- 配置快照版本发布的仓库-->
<snapshotRepository>
<id>nexus-snapshots</id>
<name>Nexus Snapshots Repository</name>
<url>http://192.168.1.204:8081/nexus/content/repositories/snapshots/</url>
</snapshotRepository>
<!-- 配置release版本发布的仓库-->
<repository>
<id>nexus-releases</id>
<name>Nexus Releases Repository</name>
<url>http://192.168.1.204:8081/nexus/content/repositories/releases/</url>
</repository>
</distributionManagement>

三、常见问题

1.指定运行JDK

修改MAVEN_HOME\bin\mvn.bat
增加以下两行

set JAVA_HOME=C:\Program Files\Java\jdk1.8.0_171
set JRE_HOME=C:\Program Files\Java\jdk1.8.0_171\jre
2.发布到指定仓库

nexus默认的本地仓库有snapshots和releases两种仓库,在通过mvn deploy命令将构建文件发布到nexus的时候,会根据版本来决定发布到snapshots仓库还是releases仓库。
版本是x.x.x-Releases,则会发布到Releases仓库中;版本是x.x.x-SNAPSHOT则发布到Snapshots仓库中。

3.排除非本地区的目录或文件

可以在maven的pom.xml文件中使用正则exclude非area地区的目录

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<packagingExcludes>%regex[images/(?!${area}).*]</packagingExcludes>
</configuration>
 </plugin>

然后在jenkins项目上自定义maven命令,以下打包的时候,仅包括qy目录的文件。该方法存在一个缺陷,不能使用include和exclude实现兼容共性部分和非共性部分,以上的打包仅实现了

clean package -Darea=qy

打包结果如下:
在这里插入图片描述
以上的表达式适用于每个地区有单独的一个目录的情况,有另外一种情况是将所有地区的文件放在同一个目录下,仅是以地区编号区分开了的。
在这里插入图片描述
表达式如下

%regex[images/(?!${area}).*.png]

生成war包如下
在这里插入图片描述
其他一些更复杂的情况也可以通过表达式来实现这种不同地区的需求。

4.排除web.xml

maven在构建war包是默认web.xml是必须的,否则会在构建时报错。有一些场景不需要每次都需要将web.xml打包到war包里面,可能是本地的web.xml与实际各地区生产环境不一致等原因,需要将web.xml排除。

<!-- web 目录 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
				   <warSourceExcludes>WEB-INF/web.xml</warSourceExcludes>
				   <failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
5.排除java文件
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
		<excludes>
	<exclude>**/supervise/onlineservice/interfaces/**</exclude>
		</excludes>
</configuration>
</plugin>

四、参考资料

https://www.cnblogs.com/shihaiming/p/6410813.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值