MAVEN使用(乱整理)

时间:2021-07-13
本文章向大家介绍 MAVEN使用(乱整理),主要包括 MAVEN使用(乱整理)使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

Maven生命周期及生命周期绑定

生命周期

package 即将本地编译好的文件打包为war 或者jar

install 将打包的代码存放到本地maven仓库,可供本地其它项目依赖使用

deploy 将打包在本地仓库中的项目发到服务器,供他人依赖使用

Plugins绑定maven生命周期

新建一个plugin工具后,通过绑定生命周期,maven--plugin的groupId为org.apache.maven.plugins一般都有默认的生命周期**。如maven-jar-plugin

无法在maven中引用的lib包处理

Idea使用时报错处理

Maven编译打包报错时处理

Maven项目Version版本自动写入文件

resources目录下 新增文件version

内容复制为:

#${project.artifactId}#${project.version}#${project.packaging}#${project.timestamp}#

截图示例:(下面的是目录结构及version中包含的内容)

Properties配置

<properties>
  <!-- 日期显示格式 -->
  <maven.build.timestamp.format>yyyy-MM-dd HH:mm:ss</maven.build.timestamp.format>
  <!-- 对应version里面显示的project.timestamp -->
  <project.timestamp>${maven.build.timestamp}</project.timestamp>
  <!-- 文件拷贝时的编码 -->
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  <!-- 编译时的编码 -->
  <maven.compiler.encoding>UTF-8</maven.compiler.encoding>
  <!-- maven方式跳过maven test, 等同$ mvn package -Dmaven.test.skip=true -->
  <maven.test.skip>true</maven.test.skip>
</properties>

Build配置

<build>
  <finalName>${artifactId}</finalName>
  <plugins>
    <!-- 编译配置,source源码版本target编译器版本1.7表示jdk7 -->
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
      <version>3.6.1</version>
      <configuration>
        <encoding>UTF-8</encoding>
        <source>1.7</source>
        <target>1.7</target>
      </configuration>
    </plugin>
    <!-- 编译帮助工具,这里用作时区转换工具 -->
    <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>build-helper-maven-plugin</artifactId>
      <executions>
        <execution>
          <goals>
            <!-- 执行这个命令timestamp-property -->
            <goal>timestamp-property</goal>
          </goals>
          <!-- 生命周期 validate时执行-->
          <phase>validate</phase>
          <configuration>
            <!-- 替换project.timestamp的值(没有这里时区是UTC)-->
            <name>project.timestamp</name>
            <pattern>${maven.build.timestamp.format}</pattern>
            <timeZone>GMT+8</timeZone>
          </configuration>
        </execution>
      </executions>
    </plugin>
    <!-- 处理资源文件 -->
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-resources-plugin</artifactId>
      <executions>
        <execution>
          <!-- 生命周期 validate时执行-->
          <phase>validate</phase>
          <goals>
            <goal>copy-resources</goal>
          </goals>
          <configuration>
            <outputDirectory>${project.build.directory}/${project.build.finalName}</outputDirectory>
            <resources>
              <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
                <includes>
                  <!-- 选择文件-->
                  <include>version</include>
                </includes>
              </resource>
            </resources>
          </configuration>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

Maven常用工具

jar包打包(需依赖外部lib)maven-jar-plugin

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-jar-plugin</artifactId>
  <configuration>
    <archive>
      <manifest>
        <addClasspath>true</addClasspath>
        <classpathPrefix>lib/</classpathPrefix>
        <mainClass>com.newland.buscontrol.message.MainServer</mainClass>
      </manifest>
    </archive>
    <outputDirectory>../deploy/release</outputDirectory>
  </configuration>
</plugin>

Jar单文件打包(无需依赖)maven-assembly-plugin

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-assembly-plugin</artifactId>
  <executions>
    <execution>
      <phase>package</phase>
      <goals>
        <goal>assembly</goal>
      </goals>
    </execution>
  </executions>
  <configuration>
    <archive>
      <manifest>
        <mainClass>${mainClass}</mainClass>
      </manifest>
    </archive>
    <appendAssemblyId>false</appendAssemblyId>
    <descriptorRefs>
      <descriptorRef>jar-with-dependencies</descriptorRef>
    </descriptorRefs>
    <outputDirectory>../deploy/release</outputDirectory>
  </configuration>
</plugin>

配置清理及不清理的文件maven-clean-plugin

(与绑定生命周期一起用时,可以在package时清理一些指定文件)**

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-clean-plugin</artifactId>
  <configuration>
    <filesets>
      <fileset>
        <directory>deploy/release</directory>
        <excludes>
          <exclude>BusControl.properties</exclude>
          <exclude>排除文件.jar</exclude>
        </excludes>
      </fileset>
    </filesets>
  </configuration>
</plugin>

多模块项目(重复工作放在一个模块下)

多模块展示及创建方法(主模块右击新建模块)

指定项目中公有的模块

父pom指定公用模块

<!--指定项目中公有的模块-->
<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>com.newland</groupId>
      <artifactId>Utils</artifactId>
      <version>${project.version}</version>
    </dependency>
  </dependencies>
</dependencyManagement>

子模块使用(可以不用指定版本号)

<dependencies>
   <dependency>
        <groupId>com.newland</groupId>
        <artifactId>Utils</artifactId>
    </dependency>
</dependencies>

指定项目公共插件及插件公共配置

父pom指定公共插件

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-assembly-plugin</artifactId>
      <executions>
        <execution>
          <phase>package</phase>
          <goals>
            <goal>assembly</goal>
          </goals>
        </execution>
      </executions>
      <configuration>
        <appendAssemblyId>false</appendAssemblyId>
        <descriptorRefs>
          <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
        <outputDirectory>../deploy/release</outputDirectory>
      </configuration>
    </plugin>
  </plugins>
</build>

子模块使用(不需版本号与其它公共配置)

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-assembly-plugin</artifactId>
  <configuration>
    <archive>
      <manifest>
        <mainClass>${mainClass}</mainClass>
      </manifest>
    </archive>
  </configuration>
</plugin>

Maven模块引用配置

环境选择classifier(json-lib)

<dependency>
  <groupId>net.sf.json-lib</groupId>
  <artifactId>json-lib</artifactId>
  <version>2.4</version>
  <classifier>jdk15</classifier>
</dependency>

Jar包冲突之运行环境冲突(使用provided)

在运行环境中存在(tomcat中)且项目中需要直接使用的包,如果直接加maven引用会导致jar冲突。

<dependency>
  <groupId>javax.servlet</groupId>
  <artifactId>javax.servlet-api</artifactId>
  <version>3.0.1</version>
  <scope>provided</scope>
</dependency>
<dependency>
  <groupId>javax.servlet.jsp</groupId>
  <artifactId>jsp-api</artifactId>
  <version>2.1</version>
  <scope>provided</scope>
</dependency>

**Maven ** 引用的jar包导致的冲突(dependencies工具)

可以直接用exclusions排除,或者使用show dependencies查看冲突关系

<dependency>
  <groupId>org.apache.struts</groupId>
  <artifactId>struts-core</artifactId>
  <version>1.3.10</version>
  <exclusions>
    <exclusion>
      <artifactId>org.apache.commons</artifactId>
      <groupId>commons-lang3</groupId>
    </exclusion>
  </exclusions>
</dependency>

原文地址:https://www.cnblogs.com/mixley/p/15008288.html