Maven 配置使用及常用插件说明

文章目录

  • 1 常用命令
  • 2 使用 Archetype 生成项目
  • 3 Resource-Filters
  • 4 手动提交 jar 到 remote repository
  • 5 assembly插件实现自定义打包
  • 6 配置 Java 编译器版本
  • 7 JUnit5 Pom.xml 配置
  • 8 使用 maven-dependency-plugin 来拷贝依赖 jar 包到指定目录

https://books.sonatype.com/mvnex-book/reference/index.html

1 常用命令

  • mvn validate
  • mvn compile
  • mvn test
  • mvn package
  • mvn install
  • mvn denpendency:analyze : 能检测两类错误,1、发现未使用的 jar;2、发现已使用但没有显示引用的 jar;

参数

  • -Dmaven.test.skip=true 不运行单元测试
  • -Pdev 运行时指定 profile

2 使用 Archetype 生成项目

1、quickstart

mvn archetype:generate -DartifactId=my-app -DgroupId=com.mycompany.app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

2、webapp

mvn archetype:generate -DartifactId=my-web-app -DgroupId=com.mycompany.app -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false

web-app 项目结构如下:

├───src
│ └───main
│ ├───resources
│ └───webapp
│ └───WEB-INF
└───target
├───classes
├───maven-archiver
└───my-web-app
├───META-INF
└───WEB-INF
└───classes

3 Resource-Filters

在 resources 目录中的文件,可以使用占位符,比如${name}。这些变量的值实际定义在以下文件中:

  1. pom.xml profile settings
  2. pom.xml properties, ${project.name}
  3. main/filters/name.properties
  4. System property
  5. settings.xml
  6. environment settings, ${env.name}

在 pom.xml 中开启功能:

<build>
 <resources>
  <resource>
   <directory>src/main/resourcesdirectory>
   <filtering>truefiltering>
   resource>
 resources>
build>

构建时,在 process-resources 阶段,maven 会将占位符替换为实际的值。

4 手动提交 jar 到 remote repository

有时,远端的 maven repository 没有项目所需的 jar,并且开发环境无法联网,这个时候需要手动提交 jar 到 remote repository 中。

mvn install:install-file -Dfile=non-maven-proj.jar -DgroupId=some.group -DartifactId=non-maven-proj -Dversion=1 -Dpackaging=jar

构建的项目可以通过 mvn install 来发布到配置的 repository 中。

5 assembly插件实现自定义打包

http://maven.apache.org/plugins/maven-assembly-plugin/index.html
https://blog.csdn.net/defonds/article/details/43233131

plugin function
maven-jar-plugin maven 默认打包插件,用来创建 project jar
maven-shade-plugin 用来打可执行包,executable(fat) jar
maven-assembly-plugin 支持定制化打包方式,例如 apache 项目的打包方式
<plugin>
    <groupId>org.apache.maven.pluginsgroupId>
    <artifactId>maven-shade-pluginartifactId>
    <version>1.2.1version>
    <executions>
        <execution>
            <phase>packagephase>
            <goals>
               <goal>shadegoal>
            goals>
                <configuration>
                    <transformers>
                        <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                            <mainClass>com.demo.AppmainClass>
                        transformer>
                    transformers>
                configuration>
        execution>
    executions>
 plugin>
<build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-pluginartifactId>
                <version>2.3.2version>
                <configuration>
                    <source>1.8source>
                    <target>1.8target>
                configuration>
            plugin>

            <plugin>
                   <artifactId> maven-assembly-plugin artifactId>
                   <configuration>
                        <descriptorRefs>
                             <descriptorRef>jar-with-dependenciesdescriptorRef>
                        descriptorRefs>
                        <archive>
                             <manifest>
                                  <mainClass>com.demo.AppmainClass>
                             manifest>
                        archive>
                   configuration>
                   <executions>
                        <execution>
                             <id>make-assemblyid>
                             <phase>packagephase>
                             <goals>
                                  <goal>singlegoal>
                             goals>
                        execution>
                   executions>
              plugin>
            

        plugins>
    build>

mvn assembly:assembly

6 配置 Java 编译器版本

<plugin>
   <groupId>org.apache.maven.pluginsgroupId>
   <artifactId>maven‑compiler‑pluginartifactId>
   <version>3.6.1version>
   <configuration>
       <source>1.8source>
       <target>1.8target>
   configuration>
plugin>

7 JUnit5 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/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0modelVersion>

	<groupId>com.examplegroupId>
	<artifactId>junit5-jupiter-starter-mavenartifactId>
	<version>1.0-SNAPSHOTversion>

	<properties>
		<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
		<maven.compiler.source>1.8maven.compiler.source>
		<maven.compiler.target>${maven.compiler.source}maven.compiler.target>

		<junit.jupiter.version>5.3.2junit.jupiter.version>
	properties>

	<dependencies>
		<dependency>
			<groupId>org.junit.jupitergroupId>
			<artifactId>junit-jupiter-apiartifactId>
			<version>${junit.jupiter.version}version>
			<scope>testscope>
		dependency>
		<dependency>
			<groupId>org.junit.jupitergroupId>
			<artifactId>junit-jupiter-paramsartifactId>
			<version>${junit.jupiter.version}version>
			<scope>testscope>
		dependency>
		<dependency>
			<groupId>org.junit.jupitergroupId>
			<artifactId>junit-jupiter-engineartifactId>
			<version>${junit.jupiter.version}version>
			<scope>testscope>
		dependency>
	dependencies>

	<build>
		<plugins>
			
			<plugin>
				<artifactId>maven-surefire-pluginartifactId>
				<version>2.22.1version>
			plugin>
		plugins>
	build>

project>

junit-platform-surefire-provider 已经 deprecated 了,Maven Surefire 从 2.22.0 版本开始对 JUnit Platform 提供 native support。

对于 2.22.0 之前的版本,可以参考下面的 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/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0modelVersion>

	<groupId>com.makotojava.learngroupId>
	<artifactId>HelloJUnit5artifactId>
	<version>1.0.2version>
	<packaging>jarpackaging>

	<name>HelloJUnit5name>
	<url>http://maven.apache.orgurl>

	<properties>
		<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
		<junit.platform.version>1.0.2junit.platform.version>
		<junit.jupiter.version>5.0.2junit.jupiter.version>
		<junit.vintage.version>4.12.2junit.vintage.version>
		<logback.version>1.2.3logback.version>
		<java.version>1.8java.version>
	properties>

	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.pluginsgroupId>
				<artifactId>maven-compiler-pluginartifactId>
				<version>3.6.1version>
				<configuration>
					<source>${java.version}source>
					<target>${java.version}target>
				configuration>
			plugin>
			<plugin>
				<artifactId>maven-surefire-pluginartifactId>
				<version>2.19version>
				<dependencies>
					<dependency>
						<groupId>org.junit.platformgroupId>
						<artifactId>junit-platform-surefire-providerartifactId>
						<version>${junit.platform.version}version>
					dependency>
					<dependency>
						<groupId>org.junit.jupitergroupId>
						<artifactId>junit-jupiter-engineartifactId>
						<version>${junit.jupiter.version}version>
					dependency>
				dependencies>
			plugin>
		plugins>
	build>

	<dependencies>
		<dependency>
			<groupId>org.junit.jupitergroupId>
			<artifactId>junit-jupiter-engineartifactId>
			<version>${junit.jupiter.version}version>
			<scope>testscope>
		dependency>
		<dependency>
			<groupId>org.junit.platformgroupId>
			<artifactId>junit-platform-runnerartifactId>
			<version>${junit.platform.version}version>
			<scope>testscope>
		dependency>
		<dependency>
			<groupId>ch.qos.logbackgroupId>
			<artifactId>logback-classicartifactId>
			<version>${logback.version}version>
		dependency>
	    <dependency>
      		<groupId>org.junit.platformgroupId>
	      	<artifactId>junit-platform-console-standaloneartifactId>
	      	<version>${junit.platform.version}version>
	      	<scope>testscope>
	    dependency>
	dependencies>

project>

8 使用 maven-dependency-plugin 来拷贝依赖 jar 包到指定目录

			<plugin>
				<groupId>org.apache.maven.pluginsgroupId>
				<artifactId>maven-dependency-pluginartifactId>
				<version>3.0.1version>
				<executions>
					<execution>
						<id>copy-dependenciesid>
						<phase>testphase>
						<goals>
							<goal>copy-dependenciesgoal>
						goals>
						<configuration>
							<outputDirectory>${project.build.directory}/liboutputDirectory>
						configuration>
					execution>
				executions>
			plugin>

你可能感兴趣的:(Java)