`
liugang594
  • 浏览: 977730 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

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可用的配置如下:

必选

Name Type Since Description
artifacts Artifact[] 1.0 Attach an array of artifacts to the project.

可选

Name Type Since Description
basedir String 1.5 This project's base directory.
Default value is${basedir}.
runOnlyAtExecutionRoot boolean 1.5 This 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.
skipAttach boolean 1.6 This 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}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics