Maven插件

Maven插件

一、maven-svn-revision-number-plugin

1.1、实现功能

1、获取当前工程主目录下svn版本号。
2、获取当前工程指定目录的svn版本号。

1.2、配置结构

<configuration>
    <entries>
        <entry>
            <path>${project.basedir}</path>
            <prefix>${project.artifactId}</prefix>
            <depth>infinity</depth>
            <reportUnversioned>true</reportUnversioned>
            <reportIgnored>false</reportIgnored>
            <reportOutOfDate>false</reportOutOfDate>
        </entry>
    </entries>
</configuration>

可以看到entries下面嵌套了一个entry。每个entry又有一个prefix的定义。也就是说他支持去获取多个目录的svn信息的。你只要配置多个entry就行了。不同的entry中的变量通过你定义的不同的prefix去拿到就行。例子如下定义了两个entry

 <plugin>
    <groupId>com.google.code.maven-svn-revision-number-plugin</groupId>
    <artifactId>svn-revision-number-maven-plugin</artifactId>
    <version>1.13</version>
    <executions>
        <execution>
            <phase>validate</phase>
            <goals>
                <goal>revision</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <entries>
            <entry>
                <prefix>svn1</prefix>
            </entry>
            <entry>
                <path>${project.basedir}/src/main/resources/META-INF/resources/demo</path>
                <prefix>svn2</prefix>
            </entry>
        </entries>
    </configuration>
    <dependencies>
        <dependency>
            <groupId>org.tmatesoft.svnkit</groupId>
            <artifactId>svnkit</artifactId>
            <version>1.9.3</version>
        </dependency>
    </dependencies>
</plugin>

如下代码,成功拿到了demo路径下的svn版本号。并且写入了manifest中。

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>${maven-jar-plugin-version}</version>
    <configuration>
        <archive>
            <compress>true</compress>
            <addMavenDescriptor>false</addMavenDescriptor>
            <manifest>
                <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
                <addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
            </manifest>
            <manifestEntries>
                <SVN-Revision>${svn1.revision}</SVN-Revision>
                <SVN-CommittedRevision>${svn.committedRevision}</SVN-CommittedRevision>
                <Build-Time>${buildTime}</Build-Time>
                <Demo-SVN-CommittedRevision>${svn2.committedRevision}</Demo-SVN-CommittedRevision>
            </manifestEntries>
        </archive>
    </configuration>
</plugin>

${svn1.XXXXX}到底能获取到哪些信息:

repository = ${prefix.repository}
path = ${prefix.path}
revision = ${prefix.revision}
mixedRevisions = ${prefix.mixedRevisions}
committedRevision = ${prefix.committedRevision}
committedDate = ${prefix.committedDate}
status = ${prefix.status}
specialStatus = ${prefix.specialStatus}

一个示例:

<!-- 读取svn版本插件-->
<plugin>
    <groupId>com.google.code.maven-svn-revision-number-plugin</groupId>
    <artifactId>svn-revision-number-maven-plugin</artifactId>
    <version>1.13</version>
    <executions>
        <execution>
            <goals>
                <goal>revision</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <entries>
            <entry>
                <prefix>svn</prefix>
            </entry>
        </entries>
    </configuration>
    <dependencies>
        <dependency>
            <groupId>org.tmatesoft.svnkit</groupId>
            <artifactId>svnkit</artifactId>
            <version>1.8.10</version>
        </dependency>
    </dependencies>
</plugin>

二、buildnumber-maven-plugin

<!--构建时间生成插件-->
<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>buildnumber-maven-plugin</artifactId>
    <version>1.4</version>
    <!--插件的公用配置,下面执行execution时还可以增加配置,或者覆盖这里的配置-->
    <configuration>
        <timezone>GMT+8</timezone>
    </configuration>
    <executions>
        <!--执行目标1:生成格式yyyy/MM/dd HH:mm:ss的构建时间,并设置到属性buildTime中-->
        <execution>
            <id>create-build-time</id>
            <goals>
                <goal>create-timestamp</goal>
            </goals>
            <configuration>
                <timestampPropertyName>buildTime</timestampPropertyName>
                <timestampFormat>yyyy/MM/dd HH:mm:ss</timestampFormat>
            </configuration>
        </execution>
        <!--执行目标2:生成格式${appVersion}.yyMMdd的构建版本号,并设置到属性buildVersion中-->
        <execution>
            <id>create-build-version</id>
            <goals>
                <goal>create-timestamp</goal>
            </goals>
            <configuration>
                <timestampPropertyName>buildVersion</timestampPropertyName>
                <timestampFormat>${application.version}.yyMMdd</timestampFormat>
            </configuration>
        </execution>
    </executions>
    <inherited>false</inherited>
</plugin>

三、maven-resources-plugin

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-resources-plugin</artifactId>
    <executions>
        <execution>
            <id>copy-tools</id>
            <phase>package</phase>
            <goals>
                <goal>copy-resources</goal>
            </goals>
            <configuration>
                <resources>
                    <resource>
                        <directory>../env-resources/tools</directory>
                        <includes>
                            <include>cosign</include>
                        </includes>
                    </resource>
                </resources>
                <outputDirectory>../jars/tools/</outputDirectory>
            </configuration>
        </execution>
    </executions>
    <version>3.1.0</version>
</plugin>

四、实践

示例1

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
</plugin>

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <configuration>
        <outputDirectory>${project.build.directory}/inkblog/bin</outputDirectory>
        <archive>
            <manifest>
                <addClasspath>true</addClasspath>
                <classpathPrefix>../lib/</classpathPrefix>
                <mainClass>com.payn.InkblogApplication</mainClass>
            </manifest>
        </archive>
    </configuration>
</plugin>

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
        <execution>
            <id>copy-dependencies</id>
            <phase>prepare-package</phase>
            <goals>
                <goal>copy-dependencies</goal>
            </goals>
            <configuration>
                <outputDirectory>${project.build.directory}/blog/lib</outputDirectory>
                <overWriteReleases>false</overWriteReleases>
                <overWriteSnapshots>false</overWriteSnapshots>
                <overWriteIfNewer>true</overWriteIfNewer>
            </configuration>
        </execution>
    </executions>
</plugin>

生成的META-INF/MANIFEST.MF

Manifest-Version: 1.0
Implementation-Title: inkblog
Implementation-Version: 0.0.1-SNAPSHOT
Built-By: 桥下之民
Implementation-Vendor-Id: com.payn
Class-Path: ../lib/spring-boot-starter-jdbc-2.0.3.RELEASE.jar ../lib/s
 pring-boot-starter-2.0.3.RELEASE.jar 
Created-By: Apache Maven 3.3.9
Build-Jdk: 1.8.0_144
Implementation-URL: https://projects.spring.io/spring-boot/#/spring-bo
 ot-starter-parent/inkblog
Main-Class: com.payn.InkblogApplication

示例2

<!--构建时间生成插件-->
<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>buildnumber-maven-plugin</artifactId>
    <version>1.4</version>
    <!--插件的公用配置,下面执行execution时还可以增加配置,或者覆盖这里的配置-->
    <configuration>
        <timezone>GMT+8</timezone>
    </configuration>
    <executions>
        <!--执行目标1:生成格式yyyy/MM/dd HH:mm:ss的构建时间,并设置到属性buildTime中-->
        <execution>
            <id>create-build-time</id>
            <goals>
                <goal>create-timestamp</goal>
            </goals>
            <configuration>
                <timestampPropertyName>buildTime</timestampPropertyName>
                <timestampFormat>yyyy/MM/dd HH:mm:ss</timestampFormat>
            </configuration>
        </execution>
        <!--执行目标2:生成格式${appVersion}.yyMMdd的构建版本号,并设置到属性buildVersion中-->
        <execution>
            <id>create-build-version</id>
            <goals>
                <goal>create-timestamp</goal>
            </goals>
            <configuration>
                <timestampPropertyName>buildVersion</timestampPropertyName>
                <timestampFormat>${application.version}.yyMMdd</timestampFormat>
            </configuration>
        </execution>
    </executions>
    <inherited>false</inherited>
</plugin>

<!--Maven默认生成jar插件,用于添加清单(MANIFEST)内容-->
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <configuration>
        <archive>
            <manifestEntries>
                <!--程序名称-->
                <Implementation-Title>${project.name}</Implementation-Title>
                <!--发布版本-->
                <Implementation-Version>${application.publish.version}</Implementation-Version>
                <!--构建版本-->
                <!--suppress UnresolvedMavenProperty -->
                <Build-Version>${buildVersion}</Build-Version>
                <!--构建时设置的环境dev/proc-->
                <Build-Profile>${profiles.active}</Build-Profile>
                <!--构建(打包)时间-->
                <!--suppress UnresolvedMavenProperty -->
                <Build-Time>${buildTime}</Build-Time>
                <!--构建SVN点-->
                <!--suppress UnresolvedMavenProperty -->
                <SVN-Version>${svn.revision}</SVN-Version>
            </manifestEntries>
        </archive>
    </configuration>
</plugin>

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-resources-plugin</artifactId>
    <executions>
        <execution>
            <id>copy-tools</id>
            <phase>package</phase>
            <goals>
                <goal>copy-resources</goal>
            </goals>
            <configuration>
                <resources>
                    <resource>
                        <directory>../env-resources/tools</directory>
                        <includes>
                            <include>cosign</include>
                        </includes>
                    </resource>
                </resources>
                <outputDirectory>../jars/tools/</outputDirectory>
            </configuration>
        </execution>
    </executions>
    <version>3.1.0</version>
</plugin>

生成的META-INF/MANIFEST.MF

Manifest-Version: 1.0
Implementation-Title: inkblog
Implementation-Version: 4.6.0.230310
Built-By: 桥下之民
Implementation-Vendor-Id: com.payn
Build-Time: 2023/04/03 20:42:47
Build-Profile: dev
Created-By: Apache Maven 3.3.9
Build-Jdk: 1.8.0_144
Implementation-URL: https://projects.spring.io/spring-boot/#/spring-bo
 ot-starter-parent/inkblog
Build-Version: 4.6.0.230403
SVN-Version: 

评论

暂无

添加新评论