已复制
全屏展示
复制代码

掌握Maven项目管理工具


· 12 min read

本文将在项目开发中常见的 Maven 需求总结成单个的小项,按照需求查阅目录即可。

指定Java编译版本

  • 方法1
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>11</source>
                <target>11</target>
            </configuration>
        </plugin>
    </plugins>
</build>
  • 方法2
<properties>
    <maven.compiler.source>11</maven.compiler.source>
    <maven.compiler.target>11</maven.compiler.target>
    <project.build.sourceEncoding>utf-8</project.build.sourceEncoding>
</properties>

只解决依赖不打包

mvn dependency:resolve

添加socks5代理

  • 方法1:使用 maven 的配置 ~/.m2/settings.xml
<settings>
    <proxies>
        <proxy>
            <id>socks5</id>
            <active>true</active>
            <protocol>socks5</protocol>
            <username></username>
            <password></password>
            <host>127.0.0.1</host>
            <port>1090</port>
            <nonProxyHosts>localhost|127.0.0.1</nonProxyHosts>
        </proxy>
    </proxies>
</settings>
  • 方法2:使用 proxychains 工具
# 配置好 proxychains 以后直接使用
proxychains4 mvn clean package

添加全局仓库源配置

现在下面的 4 个中选一个,或者全部都使用。

  • ~/.m2/setting.xml
<settings>
    <mirrors>
        <mirror>
            <id>aliyunmaven</id>
            <mirrorOf>*</mirrorOf>
            <name>阿里云公共仓库</name>
            <url>https://maven.aliyun.com/repository/public</url>
        </mirror>
        <mirror>
            <id>nexus-tencentyun</id>
            <mirrorOf>*</mirrorOf>
            <name>Nexus tencentyun</name>
            <url>http://mirrors.cloud.tencent.com/nexus/repository/maven-public/</url>
        </mirror>
        <mirror>
            <id>huaweicloud</id>
            <mirrorOf>*</mirrorOf>
            <url>https://mirrors.huaweicloud.com/repository/maven/</url>
        </mirror>
        <mirror>
            <id>nexus-163</id>
            <mirrorOf>*</mirrorOf>
            <name>Nexus 163</name>
            <url>http://mirrors.163.com/maven/repository/maven-public/</url>
        </mirror>
    </mirrors>
</settings>

添加项目的仓库地址

在 pom.xml 文件中加入仓库的信息,加入仓库信息以后这个项目在任何地方打包时都使用该配置,都能打包成功。

基础仓库


    <repositories>
        <repository>
            <id>central</id>
            <url>https://maven.aliyun.com/repository/central</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>public</id>
            <url>https://maven.aliyun.com/repository/public</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
    </repositories>

附加仓库

根据实际情况添加

    <repositories>
        <repository>
            <id>google</id>
            <url>https://maven.aliyun.com/repository/google</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>gradle-plugin</id>
            <url>https://maven.aliyun.com/repository/gradle-plugin</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>spring</id>
            <url>https://maven.aliyun.com/repository/spring</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>spring-plugin</id>
            <url>https://maven.aliyun.com/repository/spring-plugin</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>grails-core</id>
            <url>https://maven.aliyun.com/repository/grails-core</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>apache-snapshots</id>
            <url>https://maven.aliyun.com/repository/apache snapshots</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
    </repositories>

打包指定默认类名

在打包的时候添加如下的 pugin,这样可以指定运行时默认的类名,运行时就可以不知道类名了。

</dependencies>
    <dependency>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>3.2.4</version>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <transformers>
                            <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                <mainClass>cn.inetflow.HelloWorld</mainClass>
                            </transformer>
                        </transformers>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
  • 运行时运行使用 -jar 参数:java -jar fulltimejob-1.0-SNAPSHOT.jar

打包所有依赖

有时jar包的运行环境没有部分特定的包,比如hdfs的依赖包,这时将所有的依赖都打包在一起,可以使用如下配置。

</dependencies>
    <dependency>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>3.3.0</version>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
        </plugin>
    </plugins>
</build>

打包排除掉资源文件

排除掉资源文件的目的:在正式运行jar包时,使用自定义的资源配置文件,而不是打到jar包里面。

<build>

    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <excludes>
                <exclude>*.properties</exclude>
                <exclude>*.xml</exclude>
            </excludes>
        </resource>
    </resources>

</build>

拷贝资源文件

下面示例是拷贝到新目录:etc目录、bin目录

<dependency>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-resources-plugin</artifactId>
    <version>3.2.0</version>
</dependency>

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-resources-plugin</artifactId>
    <configuration>
        <encoding>UTF-8</encoding>
    </configuration>
    <executions>
        <execution>
            <id>copy-resources</id>
            <phase>package</phase>
            <goals>
                <goal>copy-resources</goal>
            </goals>
            <configuration>
                <outputDirectory>${build.directory}/${project.name}-${version}/etc</outputDirectory>
                <resources>
                    <resource>
                        <directory>src/main/resources</directory>
                        <exclude></exclude>
                    </resource>
                </resources>
            </configuration>
        </execution>
        <execution>
            <id>copy-binaries</id>
            <phase>package</phase>
            <goals>
                <goal>copy-resources</goal>
            </goals>
            <configuration>
                <outputDirectory>${build.directory}/${project.name}-${version}/bin</outputDirectory>
                <resources>
                    <resource>
                        <directory>src/bin</directory>
                        <exclude></exclude>
                    </resource>
                </resources>
            </configuration>
        </execution>
    </executions>
</plugin>

拷贝其他依赖到lib目录

拷贝的 lib 目录,在运行项目时通过 -cp 参数指定 classpath

<dependency>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <version>3.1.2</version>
</dependency>
        
<plugin>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
        <execution>
            <id>copy-dependencies</id>
            <phase>prepare-package</phase>
            <goals>
                <goal>copy-dependencies</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <includeTypes>jar</includeTypes>
        <overWriteSnapshots>true</overWriteSnapshots>
        <type>jar</type>
        <outputDirectory>${build.directory}/${project.name}-${version}/share/lib</outputDirectory>
    </configuration>
</plugin>

指定打包路径与添加classpath

  • 指定打包路径就是指定最终生成的jar包的路径。
  • 添加classpath表示:只要lib与生成的jar包路径不变,则在运行jar包时,不需要指定lib目录到classpath了。注意:classpathPrefix是相对路径。
<dependency>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>3.2.0</version>
</dependency>

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <configuration>
        <archive>
            <manifest>
                <addClasspath>true</addClasspath>
                <classpathPrefix>lib</classpathPrefix>
            </manifest>
        </archive>
        <outputDirectory>${build.directory}/${project.name}-${version}/share</outputDirectory>
    </configuration>
</plugin>

# 即 hellojava-0.0.1.jar 和 lib 在同级目录

$ ll target/hellojava-0.0.1/share/
-rw-r--r--   1 yzy  staff  4436 Oct 28 11:06 hellojava-0.0.1.jar
drwxr-xr-x  82 yzy  staff  2624 Oct 28 11:06 lib

scala java 混合使用

  • 目录结构
$ tree src
src
├── main
│   ├── java
│   │   └── org
│   │       └── example
│   │           └── MainHello.java
│   ├── resources
│   └── scala
│       └── org
│           └── example
│               └── HelloWorld.scala
└── test
    └── java
  • MainHello.java
package org.example;

import org.example.HelloWorld;

public class MainHello {
    public static void main(String[] args) {
        System.out.println("Main hello 66666");
        HelloWorld.main(new String[]{""});
    }
}
  • HelloWorld.scala
package org.example

object HelloWorld {

  def main(args: Array[String]): Unit = {
    println("this is scala")
  }

}
  • 添加插件maven-scala-plugin,注意scala、java的兼容版本问题,两者必须要匹配上才能编译成功。
<?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>org.example</groupId>
    <artifactId>scalajava</artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <project.build.sourceEncoding>utf-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.scala-tools</groupId>
            <artifactId>maven-scala-plugin</artifactId>
            <version>2.15.1</version>
        </dependency>
        <dependency>
            <groupId>org.scala-lang</groupId>
            <artifactId>scala-library</artifactId>
            <version>2.11.12</version>
        </dependency>
        <dependency>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>3.1.2</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.scala-tools</groupId>
                <artifactId>maven-scala-plugin</artifactId>
                <version>2.15.2</version>
                <executions>
                    <execution>
                        <id>scala-compile-first</id>
                        <phase>process-resources</phase>
                        <goals>
                            <goal>compile</goal>
                        </goals>
                        <configuration>
                            <includes>
                                <include>**/*.scala</include>
                                <include>**/*.java</include>
                            </includes>
                        </configuration>
                    </execution>
                    <execution>
                        <id>scala-test-compile</id>
                        <goals>
                            <goal>testCompile</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <includeTypes>jar</includeTypes>
                    <overWriteSnapshots>true</overWriteSnapshots>
                    <type>jar</type>
                    <outputDirectory>${build.directory}/${project.name}-${version}/share/lib</outputDirectory>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>
  • 编译以后运行 MainHello.java 类即可,运行时需要指定依赖包、原本的类,比如
java -cp target/scalajava-1.0-SNAPSHOT.jar:./target/scalajava-1.0-SNAPSHOT/share/lib/* org.example.MainHello


仅打包子模块

Maven 多模块工程打包指定模块工程执行如下命令:

mvn clean package -am -pl指定模块工程名

参数说明:

  • -am   --also-make 同时构建所列模块的依赖模块;
  • -amd   --also-make-dependents 同时构建依赖于所列模块的模块;
  • -pl   --projects 构建制定的模块,模块间用逗号分隔;
  • -rf   --resume-from从指定的模块恢复反应堆。

打包部分依赖

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.2.4</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <artifactSet>
                        <includes>
                            <include>org.roaringbitmap:RoaringBitmap</include>
                            <include>com.alibaba:fastjson</include>
                            <include>org.projectlombok:lombok</include>
                            <include>com.esotericsoftware.kryo:kryo</include>
                        </includes>
                    </artifactSet>
                </configuration>
            </plugin>
        </plugins>
    </build>

打包排除部分依赖

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.2.4</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <artifactSet>
                        <excludes>
                            <exclude>org.roaringbitmap:RoaringBitmap</exclude>
                            <exclude>com.alibaba:fastjson</exclude>
                            <exclude>org.projectlombok:lombok</exclude>
                            <exclude>com.esotericsoftware.kryo:kryo</exclude>
                        </excludes>
                    </artifactSet>
                </configuration>
            </plugin>
        </plugins>
    </build>

springboot增加第三方Jar包到项目

参考:https://blog.51cto.com/u_12660945/5159508

  • 首先在项目中新建lib目录,把第三方的jar包放在这个目录,比如  myproject/lib/ImpalaJDBC41-2.6.12.jar
  • 新增pom依赖。
<dependency>
  <groupId>com.myjar</groupId>
  <artifactId>myjar</artifactId>
  <version>1.0</version>
  <scope>system</scope>
  <systemPath>${basedir}/lib/ImpalaJDBC41-2.6.12.jar</systemPath>
</dependency>
  • 增加打包配置
<plugin>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-maven-plugin</artifactId>
  <configuration>
    <includeSystemScope>true</includeSystemScope>
  </configuration>
</plugin>

增加第三方Jar包到项目

参考:https://blog.51cto.com/u_12660945/5159508

  • 首先在项目中新建lib目录,把第三方的jar包放在这个目录,比如  myproject/lib/ImpalaJDBC41-2.6.12.jar
  • 新增pom依赖。
<dependency>
  <groupId>com.myjar</groupId>
  <artifactId>myjar</artifactId>
  <version>1.0</version>
  <scope>system</scope>
  <systemPath>${basedir}/lib/ImpalaJDBC41-2.6.12.jar</systemPath>
</dependency>
🔗

文章推荐