build-helper-maven-plugin的使用

build-helper-maven-plugin 插件主要的作用就是用来指定额外的源码路径和资源路径。它总共有15个goal:

这里重点介绍一下attach-artifact这个goal,它的作用就是在安装或部署artifact的时候,附加的安装或部署一些其他资源或文件。

 

要使用 build-helper-maven-plugin,首先添加插件声明:

  <build>
  	<plugins>
  		<plugin>
  			<groupId>org.codehaus.mojo</groupId>
  			<artifactId>build-helper-maven-plugin</artifactId>
  			<version>1.9</version>
  		</plugin>
  	</plugins>
  </build>

然后和其他插件一样,定义它的phase和goal。

 

attach-artifact默认的phase是package:

	<build>
		<plugins>
			<plugin>
				<groupId>org.codehaus.mojo</groupId>
				<artifactId>build-helper-maven-plugin</artifactId>
				<version>1.8</version>
				<executions>
					<execution>
						<phase>package</phase>
						<goals>
							<goal>attach-artifact</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>

剩下的就是configuratino配置了。

 

attach-artifact可用的配置如下:

必选

NameTypeSinceDescription
artifactsArtifact[]1.0Attach an array of artifacts to the project.

可选

NameTypeSinceDescription
basedirString1.5This project's base directory.
Default value is${basedir}.
runOnlyAtExecutionRootboolean1.5This will cause the execution to be run only at the top of a given module tree. That is, run in the project contained in the same folder where the mvn execution was launched.
Default value isfalse.
User property isbuildhelper.runOnlyAtExecutionRoot.
skipAttachboolean1.6This allows to skip the attach execution in case it is known that the corresponding file does not exists. For exemple, when the previous ant-run task is skipped with a unless.
Default value isfalse.
User property isbuildhelper.skipAttach.

这里只有artifacts是必选的;artifacts的说明如下:

artifacts:

Attach an array of artifacts to the project.
  • Typeorg.codehaus.mojo.buildhelper.Artifact[]
  • Since1.0
  • RequiredYes

其中的每个artifact是类型org.codehaus.mojo.buildhelper.Artifact。根据Artifact Doc说明,它只有三个可配置项:

  • file:指定欲附加的文件
  • type:指定文件类型
  • classifier:指定文件的附属信息

最后生成的文件名会是:

${artifactId}-${version}-${classifier}.${type}

例如,想在安装或部署的时候附加 etc/a.properties 文件,则配置如下:

				<configuration>
					<artifacts>
						<artifact>
							<file>etc/a.properties</file>
							<type>properties</type>
							<classifier>org.liugang.settings</classifier>
						</artifact>
					</artifacts>
				</configuration>

假设当前pom的groupId/artifactId/version为: org.liugang.maven.resources/BuilderHelperTester/0.0.1-SNAPSHOT

 

则运行完 mvn clean install 之后,到本地Maven库的目录:

${M2Repository}\org\liugang\maven\resources\BuilderHelperTester\0.0.1-SNAPSHOT

下就可以看到除了生成对应的jar文件,还会生成一个文件:

BuilderHelperTester-0.0.1-SNAPSHOT-org.liugang.settings.properties

这个文件的内容即 etc/a.properties 里的内容。

然后如果要引用这个properties文件,需要指定具体的type和classifier,例如:

 

			<groupId>org.liugang.maven.resources</groupId>
			<artifactId>BuilderHelperTester</artifactId>
			<version>0.0.1-SNAPSHOT</version>
		 	<type>properties</type> 
		 	<classifier>org.liugang.settings</classifier> 

像在karaf里就可以通过这种方式读取一些bundle的额外设置:

<configfile finalname="/etc/org.liugang.settings.cfg">mvn:org.liugang.maven.resources/BuilderHelperTester/0.0.1-SNAPSHOT/properties/org.liugang.settings</configfile> 

其中路径的格式为:

mvn:${groupId}/${artifactId}/${version}/${type}/${classifier}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值