[maven] 实现&使用 plugin 及 properties 简述

这章内容,我个人感觉可看可不看……?

不过课都上了,笔记 📒 补完才对得起自己嘛

plugins

主要讲一下 maven 的 plugin 时怎么实现的,以及项目中怎么调用自己实现的 maven plugin

一般来说这个功能的确比较少用到,大多数情况下市场上已有的 plugin 是可以满足大部分需求了

新建项目

这次新建的是 plugin 项目,需要注意:

[maven] 实现&使用 plugin 及 properties 简述-LMLPHP

注意 POM 里的 packaging 类型为 maven-plugin

<?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.ga</groupId>
	<artifactId>plugindemo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>maven-plugin</packaging>

	<dependencies>
		<dependency>
			<groupId>org.apache.maven</groupId>
			<artifactId>maven-plugin-api</artifactId>
			<version>${maven-plugin-api.version}</version>
		</dependency>
		<dependency>
			<groupId>org.apache.maven.plugin-tools</groupId>
			<artifactId>maven-plugin-annotations</artifactId>
			<version>${maven-plugin-annotations.version}</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>org.apache.maven</groupId>
			<artifactId>maven-project</artifactId>
			<version>${maven-project.version}</version>
		</dependency>
	</dependencies>


	<build>
		<pluginManagement>
			<plugins>
				<plugin>
					<groupId>org.apache.maven.plugins</groupId>
					<artifactId>maven-plugin-plugin</artifactId>
					<version>${maven-plugin-plugin.version}</version>
				</plugin>
			</plugins>
		</pluginManagement>
	</build>

	<properties>
		<maven-plugin-api.version>3.6.2</maven-plugin-api.version>
		<maven-plugin-annotations.version>3.6.0</maven-plugin-annotations.version>
		<maven-project.version>2.2.1</maven-project.version>
		<maven-plugin-plugin.version>3.6.4</maven-plugin-plugin.version>
		<maven.compiler.source>17</maven.compiler.source>
		<maven.compiler.target>17</maven.compiler.target>
		<java.version>17</java.version>
	</properties>

</project>

这个是只要会用到的配置,和 eclipse 内置的 pom 比起来少了很多的内容,如 test 相关的配置全都被移除了,这里只留下需要实现 plugin 的核心模块

Java 中默认的 MOJO 如下:

/**
 * Goal which touches a timestamp file.
 */
@Mojo(name = "touch", defaultPhase = LifecyclePhase.PROCESS_SOURCES)
public class MyMojo extends AbstractMojo {
	/**
	 * Location of the file.
	 */
	@Parameter(defaultValue = "${project.build.directory}", property = "outputDir", required = true)
	private File outputDirectory;

	public void execute() throws MojoExecutionException {
	}
}

就像 POJO 代表 Plain Old Java Object,MOJO 代表的就是 Maven plain Old Java Object

可以看到实现一个 MOJO 需要 extends AbstractMojo,而里面最重要的 execute 在执行出问题的时候抛出的是 MojoExecutionException

实现 MOJO 代码

其实主要就是调用 getLog() 在命令行进行输出:

@Mojo(name = "info-renderer", defaultPhase = LifecyclePhase.COMPILE)
public class ProjectInfoMojo extends AbstractMojo {

	@Override
	public void execute() throws MojoExecutionException, MojoFailureException {
		getLog().info("Mojos are cool");
	}
}

getLog() 是由 AbstractMojo 提供的一个方法,其本身会返回一个 Log 实例以供调用,比较常见的用法就是调用 instance 上的 info, debug, error 在命令行输出相应的信息

运行 plugin

plugin 执行的语法为: mvn groupId:artifactId:version:goal

❯ mvn com.ga:plugindemo:info-renderer
[INFO] Scanning for projects...
Downloading from central: https://repo.maven.apache.org/maven2/com/ga/plugindemo/maven-metadata.xml
[INFO]
[INFO] -------------------------< com.ga:plugindemo >--------------------------
[INFO] Building plugindemo 0.0.1-SNAPSHOT
[INFO] ----------------------------[ maven-plugin ]----------------------------
[INFO]
[INFO] --- plugindemo:0.0.1-SNAPSHOT:info-renderer (default-cli) @ plugindemo ---
[INFO] Mojos are cool
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  1.026 s
[INFO] Finished at: 2023-09-19T00:28:59-04:00
[INFO] ------------------------------------------------------------------------

缩写

缩写这块我是没有成功过,不过根据官方文档 Shortening the Command Line 上说,需要修改 .m2 下的 settings.xml,大概如下:

<pluginGroups>
  <pluginGroup>sample.plugin</pluginGroup>
</pluginGroups>

接下来执行这条指令就好了:mvn projectinfo:info-renderer

相当于说把 groupId 配置到 pluginGroup 里了,不幸的是尽管在 com.ga 下面找到了自己的 plugin,但是还是没发成功运行缩写:

❯ tree ~/.m2/repository/com/ga
/Users/usr/.m2/repository/com/ga
├── maven-metadata-local.xml
├── plugindemo
│   ├── 0.0.1-SNAPSHOT
│   │   ├── _remote.repositories
│   │   ├── maven-metadata-local.xml
│   │   ├── plugindemo-0.0.1-SNAPSHOT.jar
│   │   └── plugindemo-0.0.1-SNAPSHOT.pom
│   ├── maven-metadata-local.xml
│   └── resolver-status.properties
└── resolver-status.properties

3 directories, 8 files

这有可能跟之前 xml 里的 proxy 有关,不是很确定……

不过不影响在其他项目中调用,就没继续琢磨了

调用项目信息

主要通过 annotation 实现,如下面添加了对项目的 annotation:

@Mojo(name = "info-renderer", defaultPhase = LifecyclePhase.COMPILE)
public class ProjectInfoMojo extends AbstractMojo {

	@Parameter(defaultValue = "${project}", required = true, readonly = true)
	MavenProject project;

	@Override
	public void execute() throws MojoExecutionException, MojoFailureException {
		getLog().info("Mojos are cool");
		getLog().info("Project Name: " + project.getName());
		getLog().info("Artifact Id: " + project.getArtifactId());
	}

}

终端上就能获取项目名称、artifact id 之类的信息:

❯ mvn com.ga:plugindemo:info-renderer
[INFO] Scanning for projects...
[INFO]
[INFO] -------------------------< com.ga:plugindemo >--------------------------
[INFO] Building plugindemo 0.0.1-SNAPSHOT
[INFO] ----------------------------[ maven-plugin ]----------------------------
[INFO]
[INFO] --- plugindemo:0.0.1-SNAPSHOT:info-renderer (default-cli) @ plugindemo ---
[INFO] Mojos are cool
[INFO] Project Name: plugindemo
[INFO] Artifact Id: plugindemo
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  0.313 s
[INFO] Finished at: 2023-09-19T01:56:26-04:00
[INFO] ------------------------------------------------------------------------

[maven] 实现&amp;使用 plugin 及 properties 简述-LMLPHP

访问依赖 dependencies

依赖也是保存在 project 里,这里使用了一个 lambda expression 遍历所有的依赖:

project.getDependencies().forEach(d -> getLog().info(d.toString()));

输出结果:

❯ mvn com.ga:plugindemo:info-renderer
[INFO] Scanning for projects...
[INFO]
[INFO] -------------------------< com.ga:plugindemo >--------------------------
[INFO] Building plugindemo 0.0.1-SNAPSHOT
[INFO] ----------------------------[ maven-plugin ]----------------------------
[INFO]
[INFO] --- plugindemo:0.0.1-SNAPSHOT:info-renderer (default-cli) @ plugindemo ---
[INFO] Mojos are cool
[INFO] Project Name: plugindemo
[INFO] Artifact Id: plugindemo
[INFO] Dependency {groupId=org.apache.maven, artifactId=maven-plugin-api, version=3.6.2, type=jar}
[INFO] Dependency {groupId=org.apache.maven.plugin-tools, artifactId=maven-plugin-annotations, version=3.6.0, type=jar}
[INFO] Dependency {groupId=org.apache.maven, artifactId=maven-project, version=2.2.1, type=jar}
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  0.321 s
[INFO] Finished at: 2023-09-19T01:58:10-04:00
[INFO] ------------------------------------------------------------------------

传入参数

从命令行/配置中获取参数的方法如下:

List<Dependency> dependencies = project.getDependencies();
dependencies.stream().filter(d -> scope != null && scope.equals(d.getScope()))
				.forEach(d -> getLog().info(d.toString()));

上面这个代码会获取 scope 这个参数,并且过滤掉所有与传入的 scope 不相符的依赖:

❯ mvn com.ga:plugindemo:info-renderer -Dscope=test
[INFO] Scanning for projects...
[INFO]
[INFO] -------------------------< com.ga:plugindemo >--------------------------
[INFO] Building plugindemo 0.0.1-SNAPSHOT
[INFO] ----------------------------[ maven-plugin ]----------------------------
[INFO]
[INFO] --- plugindemo:0.0.1-SNAPSHOT:info-renderer (default-cli) @ plugindemo ---
[INFO] Mojos are cool
[INFO] Project Name: plugindemo
[INFO] Artifact Id: plugindemo
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  0.365 s
[INFO] Finished at: 2023-09-19T02:03:30-04:00
[INFO] ------------------------------------------------------------------------

上面我已经提到过,所有和测试相关的 pom 都移除了,这里自然不会看到跟测试有关的 pom。而 scope 为 provided 的有一个 org.apache.maven.plugin-tools,所以就会在下面显示出来:

# change scope to provided
❯ mvn com.ga:plugindemo:info-renderer -Dscope=provided
[INFO] Scanning for projects...
[INFO]
[INFO] -------------------------< com.ga:plugindemo >--------------------------
[INFO] Building plugindemo 0.0.1-SNAPSHOT
[INFO] ----------------------------[ maven-plugin ]----------------------------
[INFO]
[INFO] --- plugindemo:0.0.1-SNAPSHOT:info-renderer (default-cli) @ plugindemo ---
[INFO] Mojos are cool
[INFO] Project Name: plugindemo
[INFO] Artifact Id: plugindemo
[INFO] Dependency {groupId=org.apache.maven.plugin-tools, artifactId=maven-plugin-annotations, version=3.6.0, type=jar}
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  0.371 s
[INFO] Finished at: 2023-09-19T02:04:30-04:00
[INFO] ------------------------------------------------------------------------

在其他项目中使用 plugin

其实和调用其他的 plugin 一样,pom 如下:

	<build>
		<plugins>
			<plugin>
				<groupId>com.ga</groupId>
				<artifactId>plugindemo</artifactId>
				<version>0.0.1-SNAPSHOT</version>
				<executions>
					<execution>
						<goals>
							<goal>info-renderer</goal>
						</goals>
					</execution>
				</executions>
				<configuration>
					<scope>test</scope>
				</configuration>
			</plugin>
		</plugins>
	</build>

执行结果如下:

[maven] 实现&amp;使用 plugin 及 properties 简述-LMLPHP

❯ mvn clean compile
[INFO] Scanning for projects...
[INFO]
[INFO] -------------------------< com.ga:use-plugin >--------------------------
[INFO] Building use-plugin 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ use-plugin ---
[INFO] Deleting /Users/usr/study/maven/use-plugin/target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ use-plugin ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /Users/usr/study/maven/use-plugin/src/main/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ use-plugin ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /Users/usr/study/maven/use-plugin/target/classes
[INFO]
[INFO] --- plugindemo:0.0.1-SNAPSHOT:info-renderer (default) @ use-plugin ---
[INFO] Mojos are cool
[INFO] Project Name: use-plugin
[INFO] Artifact Id: use-plugin
[INFO] Dependency {groupId=junit, artifactId=junit, version=4.11, type=jar}
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  1.220 s
[INFO] Finished at: 2023-09-20T20:52:37-04:00
[INFO] ------------------------------------------------------------------------

[maven] 实现&amp;使用 plugin 及 properties 简述-LMLPHP

properties

properties 这里……主要就是用到再查的感觉吧,常用的就是之前已经写过的,通过 properties 对使用的依赖进行版本管理的方法

这里用到的 maven-antrun-plugin 是打包用的插件

命令行输出所有属性

这里用的项目是之前 [maven] maven 创建 web 项目并嵌套项目 中实现的项目,POM 修改如下:

<plugins>
	<plugin>
		<groupId>org.apache.maven.plugins</groupId>
		<artifactId>maven-antrun-plugin</artifactId>
		<version>1.7</version>
		<executions>
			<execution>
				<phase>validate</phase>
				<goals>
					<goal>run</goal>
				</goals>
				<configuration>
					<tasks>
						<echoproperties />
					</tasks>
				</configuration>
			</execution>
		</executions>
	</plugin>
</plugins>

需要注意的是 echoproperties 这个属性在后来的版本被移除了,这里跟着教程使用 1.7 可以输出所有的属性:

main:
[echoproperties] #Ant properties
[echoproperties] #Wed Sep 20 21:02:03 EDT 2023
[echoproperties] java.specification.version=17
[echoproperties] ant.project.name=maven-antrun-
[echoproperties] sun.jnu.encoding=UTF-8
[echoproperties] project.build.testOutputDirectory=/Users/usr/study/maven/parent/productweb/target/test-classes
[echoproperties] project.name=productweb Maven Webapp
[echoproperties] settings.localRepository=/Users/usr/.m2/repository
[echoproperties] sun.arch.data.model=64
[echoproperties] java.vendor.url=https\://www.microsoft.com
[echoproperties] maven.dependency.org.springframework.spring-aop.jar.path=/Users/usr/.m2/repository/org/springframework/spring-aop/6.0.11/spring-aop-6.0.11.jar
[echoproperties] maven.dependency.org.springframework.spring-beans.jar.path=/Users/usr/.m2/repository/org/springframework/spring-beans/6.0.11/spring-beans-6.0.11.jar
[echoproperties] sun.boot.library.path=/Users/usr/.sdkman/candidates/java/17.0.8.1-ms/lib
[echoproperties] sun.java.command=org.codehaus.plexus.classworlds.launcher.Launcher validate
[echoproperties] jdk.debug=release
[echoproperties] maven.home=/Users/usr/.sdkman/candidates/maven/current
[echoproperties] sun.stderr.encoding=UTF-8
[echoproperties] java.specification.vendor=Oracle Corporation
[echoproperties] project.artifactId=productweb
[echoproperties] java.version.date=2023-08-24
[echoproperties] project.version=1.0
[echoproperties] java.home=/Users/usr/.sdkman/candidates/java/17.0.8.1-ms
[echoproperties] org.opentest4j\:opentest4j\:jar=/Users/usr/.m2/repository/org/opentest4j/opentest4j/1.3.0/opentest4j-1.3.0.jar
[echoproperties] basedir=/Users/usr/study/maven/parent/productweb
[echoproperties] file.separator=/
[echoproperties] java.vm.compressedOopsMode=Zero based
[echoproperties] project.packaging=war
[echoproperties] line.separator=\n
[echoproperties] org.junit.jupiter\:junit-jupiter-api\:jar=/Users/usr/.m2/repository/org/junit/jupiter/junit-jupiter-api/5.10.0/junit-jupiter-api-5.10.0.jar
[echoproperties] sun.stdout.encoding=UTF-8
[echoproperties] java.specification.name=Java Platform API Specification
[echoproperties] java.vm.specification.vendor=Oracle Corporation
[echoproperties] org.junit.platform\:junit-platform-engine\:jar=/Users/usr/.m2/repository/org/junit/platform/junit-platform-engine/1.10.0/junit-platform-engine-1.10.0.jar
[echoproperties] org.springframework\:spring-beans\:jar=/Users/usr/.m2/repository/org/springframework/spring-beans/6.0.11/spring-beans-6.0.11.jar
[echoproperties] sun.management.compiler=HotSpot 64-Bit Tiered Compilers
[echoproperties] java.runtime.version=17.0.8.1+1-LTS
[echoproperties] maven.dependency.org.springframework.spring-expression.jar.path=/Users/usr/.m2/repository/org/springframework/spring-expression/6.0.11/spring-expression-6.0.11.jar
[echoproperties] user.name=usr
[echoproperties] org.junit.jupiter\:junit-jupiter-engine\:jar=/Users/usr/.m2/repository/org/junit/jupiter/junit-jupiter-engine/5.10.0/junit-jupiter-engine-5.10.0.jar
[echoproperties] file.encoding=UTF-8
[echoproperties] guice.disable.misplaced.annotation.check=true
[echoproperties] java.vendor.version=Microsoft-8297089
[echoproperties] localRepository=\      id\: local\n      url\: file\:///Users/usr/.m2/repository/\n   layout\: default\nsnapshots\: [enabled \=> true, update \=> always]\n releases\: [enabled \=> true, update \=> always]\n
[echoproperties] java.io.tmpdir=/var/folders/j_/r1vl8z45443b115dbx6pzvn80000gq/T/
[echoproperties] org.springframework\:spring-expression\:jar=/Users/usr/.m2/repository/org/springframework/spring-expression/6.0.11/spring-expression-6.0.11.jar
[echoproperties] java.version=17
[echoproperties] java.vm.specification.name=Java Virtual Machine Specification
[echoproperties] maven.dependency.org.junit.platform.junit-platform-commons.jar.path=/Users/usr/.m2/repository/org/junit/platform/junit-platform-commons/1.10.0/junit-platform-commons-1.10.0.jar
[echoproperties] org.springframework\:spring-aop\:jar=/Users/usr/.m2/repository/org/springframework/spring-aop/6.0.11/spring-aop-6.0.11.jar
[echoproperties] native.encoding=UTF-8
[echoproperties] ant.version=Apache Ant(TM) version 1.8.2 compiled on December 20 2010
[echoproperties] java.library.path=/Users/usr/Library/Java/Extensions\:/Library/Java/Extensions\:/Network/Library/Java/Extensions\:/System/Library/Java/Extensions\:/usr/lib/java\:.
[echoproperties] java.vendor=Microsoft
[echoproperties] classworlds.conf=/Users/usr/.sdkman/candidates/maven/current/bin/m2.conf
[echoproperties] sun.io.unicode.encoding=UnicodeBig
[echoproperties] library.jansi.path=/Users/usr/.sdkman/candidates/maven/current/lib/jansi-native
[echoproperties] ant.file.maven-antrun-=/Users/usr/study/maven/parent/productweb/target/antrun/build-main.xml
[echoproperties] project.build.directory=/Users/usr/study/maven/parent/productweb/target
[echoproperties] maven.dependency.org.springframework.spring-jcl.jar.path=/Users/usr/.m2/repository/org/springframework/spring-jcl/6.0.11/spring-jcl-6.0.11.jar
[echoproperties] java.class.path=/Users/usr/.sdkman/candidates/maven/current/boot/plexus-classworlds-2.6.0.jar
[echoproperties] org.apiguardian\:apiguardian-api\:jar=/Users/usr/.m2/repository/org/apiguardian/apiguardian-api/1.1.2/apiguardian-api-1.1.2.jar
[echoproperties] java.vm.vendor=Microsoft
[echoproperties] org.apache.geronimo.specs\:geronimo-servlet_3.0_spec\:jar=/Users/usr/.m2/repository/org/apache/geronimo/specs/geronimo-servlet_3.0_spec/1.0/geronimo-servlet_3.0_spec-1.0.jar
[echoproperties] project.groupId=com.goldenaarcher.product
[echoproperties] user.timezone=America/New_York
[echoproperties] maven.conf=/Users/usr/.sdkman/candidates/maven/current/conf
[echoproperties] project.build.outputDirectory=/Users/usr/study/maven/parent/productweb/target/classes
[echoproperties] java.vm.specification.version=17
[echoproperties] os.name=Mac OS X
[echoproperties] junit.version=5.10.0
[echoproperties] ant.file.type.maven-antrun-=file
[echoproperties] sun.java.launcher=SUN_STANDARD
[echoproperties] user.country=US
[echoproperties] sun.cpu.endian=little
[echoproperties] project.build.testSourceDirectory=/Users/usr/study/maven/parent/productweb/src/test/java
[echoproperties] user.home=/Users/usr
[echoproperties] user.language=en
[echoproperties] maven.dependency.org.junit.jupiter.junit-jupiter-engine.jar.path=/Users/usr/.m2/repository/org/junit/jupiter/junit-jupiter-engine/5.10.0/junit-jupiter-engine-5.10.0.jar
[echoproperties] ant.java.version=1.7
[echoproperties] org.springframework\:spring-jcl\:jar=/Users/usr/.m2/repository/org/springframework/spring-jcl/6.0.11/spring-jcl-6.0.11.jar
[echoproperties] maven.dependency.com.goldenaarcher.product.productservices.jar.path=/Users/usr/.m2/repository/com/goldenaarcher/product/productservices/1.0/productservices-1.0.jar
[echoproperties] org.springframework\:spring-core\:jar=/Users/usr/.m2/repository/org/springframework/spring-core/6.0.11/spring-core-6.0.11.jar
[echoproperties] ant.file=/Users/usr/study/maven/parent/productweb/pom.xml
[echoproperties] path.separator=\:
[echoproperties] os.version=13.5.2
[echoproperties] java.runtime.name=OpenJDK Runtime Environment
[echoproperties] java.vm.name=OpenJDK 64-Bit Server VM
[echoproperties] com.goldenaarcher.product\:productservices\:jar=/Users/usr/.m2/repository/com/goldenaarcher/product/productservices/1.0/productservices-1.0.jar
[echoproperties] ant.core.lib=/Users/usr/.m2/repository/org/apache/ant/ant/1.8.2/ant-1.8.2.jar
[echoproperties] java.vendor.url.bug=https\://github.com/microsoft/openjdk/issues
[echoproperties] user.dir=/Users/usr/study/maven/parent
[echoproperties] maven.dependency.org.junit.platform.junit-platform-engine.jar.path=/Users/usr/.m2/repository/org/junit/platform/junit-platform-engine/1.10.0/junit-platform-engine-1.10.0.jar
[echoproperties] os.arch=x86_64
[echoproperties] maven.multiModuleProjectDirectory=/Users/usr/study/maven/parent
[echoproperties] org.junit.platform\:junit-platform-commons\:jar=/Users/usr/.m2/repository/org/junit/platform/junit-platform-commons/1.10.0/junit-platform-commons-1.10.0.jar
[echoproperties] maven.dependency.org.opentest4j.opentest4j.jar.path=/Users/usr/.m2/repository/org/opentest4j/opentest4j/1.3.0/opentest4j-1.3.0.jar
[echoproperties] maven.dependency.org.apache.geronimo.specs.geronimo-servlet_3.0_spec.jar.path=/Users/usr/.m2/repository/org/apache/geronimo/specs/geronimo-servlet_3.0_spec/1.0/geronimo-servlet_3.0_spec-1.0.jar
[echoproperties] maven.dependency.org.springframework.spring-core.jar.path=/Users/usr/.m2/repository/org/springframework/spring-core/6.0.11/spring-core-6.0.11.jar
[echoproperties] org.springframework\:spring-context\:jar=/Users/usr/.m2/repository/org/springframework/spring-context/6.0.11/spring-context-6.0.11.jar
[echoproperties] maven.dependency.org.junit.jupiter.junit-jupiter-api.jar.path=/Users/usr/.m2/repository/org/junit/jupiter/junit-jupiter-api/5.10.0/junit-jupiter-api-5.10.0.jar
[echoproperties] project.build.sourceDirectory=/Users/usr/study/maven/parent/productweb/src/main/java
[echoproperties] java.vm.info=mixed mode, sharing
[echoproperties] java.vm.version=17.0.8.1+1-LTS
[echoproperties] maven.dependency.org.springframework.spring-context.jar.path=/Users/usr/.m2/repository/org/springframework/spring-context/6.0.11/spring-context-6.0.11.jar
[echoproperties] maven.project.dependencies.versions=5.10.0\:1.10.0\:1.3.0\:1.10.0\:5.10.0\:1.1.2\:1.0\:1.0\:6.0.11\:6.0.11\:6.0.11\:6.0.11\:6.0.11\:6.0.11\:
[echoproperties] java.class.version=61.0
[echoproperties] ant.project.default-target=main
[echoproperties] maven.dependency.org.apiguardian.apiguardian-api.jar.path=/Users/usr/.m2/repository/org/apiguardian/apiguardian-api/1.1.2/apiguardian-api-1.1.2.jar
[INFO] Executed tasks
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary for productparent 1.0:
[INFO]
[INFO] productparent ...................................... SUCCESS [  0.305 s]
[INFO] productservices .................................... SUCCESS [  0.074 s]
[INFO] productweb Maven Webapp ............................ SUCCESS [  0.048 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  0.510 s
[INFO] Finished at: 2023-09-20T21:02:03-04:00
[INFO] ------------------------------------------------------------------------

[maven] 实现&amp;使用 plugin 及 properties 简述-LMLPHP

我这里应该只列出了一个项目(web)的属性……太长了,感觉拉不完……

这里主要就是提供了一些默认的 property 名称,如 project 相关信息,这个也可以在 POM 中直接使用。

下面的这个设定会直接用 artifactId 作为最终打包的名称:

<build>
	<finalName>${project.artifactId}</finalName>
</build>

[maven] 实现&amp;使用 plugin 及 properties 简述-LMLPHP

这样在 build 之后,最终的 war 文件名就是 productweb.war

还有一些其他的用法,包括访问 project.build.directory 等,这些还是在要用的时候 google 一下比较方便

除此之外它还可以访问 java 的系统属性,以及手写的一些属性,如用过不少次的 maven.compiler.source

目前最新版本的一些用法如下:

		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-antrun-plugin</artifactId>
				<version>3.1.0</version>
				<executions>
					<execution>
						<id>compile</id>
						<phase>compile</phase>
						<configuration>
							<target>
								<echo message="Project Name: ${project.name}" />
							</target>
						</configuration>
						<goals>
							<goal>run</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
		</plugins>

它可以在命令行 echo 项目名称:

[maven] 实现&amp;使用 plugin 及 properties 简述-LMLPHP

主要来说……用的不多就是了

09-30 07:34