一些常用的Maven命令和配置

时间:2022-04-09 16:29:29

1.查看pom的依赖树

mvn dependency:tree

2.查看项目的有效的pom

mvn help:effective-pom

3.查看项目的有效的settings

mvn help:effective-settings

4.deploy的时候,排除掉war所在的module

  <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.7</version>
<configuration>
<skip>true</skip>
</configuration>
</plugin>

5.deploy的时候一块发布源码包

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

【注意】在多项目构建中,将source-plugin置于顶层或parent的pom中并不会发挥作用,必须置于具体项目的pom中。

6.打包的时候设置多个资源目录


<build>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
<resource>
<directory>src/main/java</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
</resources>
</build>

7引用maven私服:

<repositories>
<repository>
<id>nexus</id>
<name>nexus</name>
<url>http://your server/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>

<pluginRepositories>
<pluginRepository>
<id>nexus</id>
<name>nexus</name>
<url>http://your server/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
8.deploy到maven私服

(1)pom.xml

<distributionManagement>
<repository>
<id>spark-release</id>
<name>Spark Release</name>
<url>http://your server/nexus/content/repositories/releases/</url>
</repository>
<snapshotRepository>
<id>spark-snapshots</id>
<name>Spark SNAPSHOTS</name>
<url>http://your server/nexus/content/repositories/snapshots/</url>
</snapshotRepository>
</distributionManagement>
(2)settings.xml:

<servers>
<server>
<id>spark-release</id>
<username>your username</username>
<password>your pasword</password>
</server>
<server>
<id>spark-snapshots</id>
<username>your username</username>
<password>your pasword</password>
</server>
</servers>
【注意】两个地方的id必须要对应起来,具体叫什么则随意。

9.项目中依赖的版本管理

(1)parent-pom.xml:

<properties>
<jstl.version>1.2</jstl.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>${jstl.version}</version>
</dependency>
<dependencies>
</dependencyManagement>
(2)各个子模块的pom.xml:

<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependencies>

9.新建项目时候的结构:

一些常用的Maven命令和配置

(1)最外层的项目的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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>demo</artifactId>
<packaging>pom</packaging>
<name>demo</name>
<url>http://maven.apache.org</url>
<parent>
<groupId>com.spark</groupId>
<artifactId>demo-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>demo-parent</relativePath>
</parent>
<modules>
<module>demo-parent</module>
<module>demo-web</module>
<module>demo-service-api</module>
<module>demo-service-impl</module>
</modules>
</project>
其中有个module作为parent,它也是所有的module的最终parent。

(2)demo-parent的pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<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.spark</groupId>
<artifactId>demo-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>demo-parent</name>
<url>http://maven.apache.org</url>

<properties>
</properties>

<repositories>
</repositories>

<pluginRepositories>
</pluginRepositories>

<distributionManagement>
</distributionManagement>

<dependencyManagement>
</dependencyManagement>

<build>
<resources>
</resources>
<plugins>
</plugins>
</build>
</project>

在parent中把所有的公共配置都定义好。

(3)demo-service-impl的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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>demo-service-impl</artifactId>
<packaging>jar</packaging>
<name>demo-service-impl</name>
<url>http://maven.apache.org</url>

<parent>
<groupId>com.spark</groupId>
<artifactId>demo-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../demo-parent</relativePath>
</parent>

<dependencies>
</dependencies>

</project>
各个module中只需要指定一下parent,然后引入自己所需要