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


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

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

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

    plugins

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

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

    新建项目

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

    在这里插入图片描述

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

    
    
    
    <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.gagroupId>
    	<artifactId>plugindemoartifactId>
    	<version>0.0.1-SNAPSHOTversion>
    	<packaging>maven-pluginpackaging>
    
    	<dependencies>
    		<dependency>
    			<groupId>org.apache.mavengroupId>
    			<artifactId>maven-plugin-apiartifactId>
    			<version>${maven-plugin-api.version}version>
    		dependency>
    		<dependency>
    			<groupId>org.apache.maven.plugin-toolsgroupId>
    			<artifactId>maven-plugin-annotationsartifactId>
    			<version>${maven-plugin-annotations.version}version>
    			<scope>providedscope>
    		dependency>
    		<dependency>
    			<groupId>org.apache.mavengroupId>
    			<artifactId>maven-projectartifactId>
    			<version>${maven-project.version}version>
    		dependency>
    	dependencies>
    
    
    	<build>
    		<pluginManagement>
    			<plugins>
    				<plugin>
    					<groupId>org.apache.maven.pluginsgroupId>
    					<artifactId>maven-plugin-pluginartifactId>
    					<version>${maven-plugin-plugin.version}version>
    				plugin>
    			plugins>
    		pluginManagement>
    	build>
    
    	<properties>
    		<maven-plugin-api.version>3.6.2maven-plugin-api.version>
    		<maven-plugin-annotations.version>3.6.0maven-plugin-annotations.version>
    		<maven-project.version>2.2.1maven-project.version>
    		<maven-plugin-plugin.version>3.6.4maven-plugin-plugin.version>
    		<maven.compiler.source>17maven.compiler.source>
    		<maven.compiler.target>17maven.compiler.target>
    		<java.version>17java.version>
    	properties>
    
    project>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55

    这个是只要会用到的配置,和 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 {
    	}
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    就像 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");
    	}
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    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] ------------------------------------------------------------------------
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    缩写

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

    <pluginGroups>
      <pluginGroup>sample.pluginpluginGroup>
    pluginGroups>
    
    • 1
    • 2
    • 3

    接下来执行这条指令就好了: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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    这有可能跟之前 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());
    	}
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    终端上就能获取项目名称、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] ------------------------------------------------------------------------
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    在这里插入图片描述

    访问依赖 dependencies

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

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

    输出结果:

    ❯ 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] ------------------------------------------------------------------------
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    传入参数

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

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

    上面这个代码会获取 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] ------------------------------------------------------------------------
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    上面我已经提到过,所有和测试相关的 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] ------------------------------------------------------------------------
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    在其他项目中使用 plugin

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

    	<build>
    		<plugins>
    			<plugin>
    				<groupId>com.gagroupId>
    				<artifactId>plugindemoartifactId>
    				<version>0.0.1-SNAPSHOTversion>
    				<executions>
    					<execution>
    						<goals>
    							<goal>info-renderergoal>
    						goals>
    					execution>
    				executions>
    				<configuration>
    					<scope>testscope>
    				configuration>
    			plugin>
    		plugins>
    	build>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    执行结果如下:

    在这里插入图片描述

    ❯ 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] ------------------------------------------------------------------------
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    在这里插入图片描述

    properties

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

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

    命令行输出所有属性

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

    <plugins>
    	<plugin>
    		<groupId>org.apache.maven.pluginsgroupId>
    		<artifactId>maven-antrun-pluginartifactId>
    		<version>1.7version>
    		<executions>
    			<execution>
    				<phase>validatephase>
    				<goals>
    					<goal>rungoal>
    				goals>
    				<configuration>
    					<tasks>
    						<echoproperties />
    					tasks>
    				configuration>
    			execution>
    		executions>
    	plugin>
    plugins>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    需要注意的是 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] ------------------------------------------------------------------------
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122

    在这里插入图片描述

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

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

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

    <build>
    	<finalName>${project.artifactId}finalName>
    build>
    
    • 1
    • 2
    • 3

    在这里插入图片描述

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

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

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

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

    		<plugins>
    			<plugin>
    				<groupId>org.apache.maven.pluginsgroupId>
    				<artifactId>maven-antrun-pluginartifactId>
    				<version>3.1.0version>
    				<executions>
    					<execution>
    						<id>compileid>
    						<phase>compilephase>
    						<configuration>
    							<target>
    								<echo message="Project Name: ${project.name}" />
    							target>
    						configuration>
    						<goals>
    							<goal>rungoal>
    						goals>
    					execution>
    				executions>
    			plugin>
    		plugins>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

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

    在这里插入图片描述

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

  • 相关阅读:
    数据库设计与建模
    HTML脚本、字符实体、URL
    Python 潮流周刊#48:Python 3.14 的发布计划
    android aidl Can‘t resolve Salary问题
    linux manual
    VSCode dlv 远程调试(备忘)
    【代码随想录】LC 704. 二分查找
    Savepoints
    dpdk ring多/单生产者、多/单消费者
    ES6 入门教程 11 对象的新增方法 11.1 Object.is() & 11.2 Object.assign()
  • 原文地址:https://blog.csdn.net/weixin_42938619/article/details/133194547