Maven插件开发

首先使用命令创建Maven工程:

mvn archetype:generate -DgroupId=groupId -DartifactId=artifactId -DarchetypeArtifactId=maven-archetype-mojo


然后想在Eclipse下编辑工程,使用下面的命令把工程进行了转化:

mvn eclipse:eclipse


完成后新建class:

@Mojo(name="instrument", requiresDependencyResolution=ResolutionScope.COMPILE, defaultPhase=LifecyclePhase.VALIDATE)
public class xxxMojo extends AbstractMojo {

	public void execute() throws MojoExecutionException {
		String nameOfRunningVM = ManagementFactory.getRuntimeMXBean().getName();
		int p = nameOfRunningVM.indexOf('@');
		String pid = nameOfRunningVM.substring(0, p);

		String jarFilePath = null;

		getLog().info("Dynamically loading xxx instrumentation...");
		try {
			jarFilePath = xxx.class.getProtectionDomain().getCodeSource().getLocation()
					.toURI().getPath().toString();

			jarFilePath = new File(jarFilePath).getCanonicalPath();

			getLog().info("Found xxx instrumentation within " + jarFilePath);
		} catch (URISyntaxException e) {
			getLog().error("Unable to find xxx instrumentation jar");
			throw new RuntimeException(e);
		} catch (IOException e) {
			getLog().error("Unable to find xxx instrumentation jar");
			throw new RuntimeException(e);
		}
		
		try {
			VirtualMachine vm = VirtualMachine.attach(pid);
			vm.loadAgent(jarFilePath, System.getProperty("xxx.AgentArgs"));
			vm.detach();
		} catch (Exception e) {
			getLog().error("Error encountered while loading the xxx agent", e);
			throw new RuntimeException(e);
		}
	}

}

修改pom.xml:

<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>groupId</groupId>
	<artifactId>artifactId</artifactId>
	<version>1.0.0</version>
	<packaging>maven-plugin</packaging>

	<name>xxx Agent Maven Plugin</name>
	<description>The xxx Agent provides run time performance monitoring for xxx applications</description>
	<url>http://www.xxx.com/</url>

	<licenses>
		<license>
			<name>The Apache Software License, Version 2.0</name>
			<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
			<distribution>repo</distribution>
		</license>
	</licenses>

	<developers>
		<developer>
			<id>zach</id>
			<name>Zach Liu</name>
			<email></email>
		</developer>
	</developers>

	<!-- <scm> <connection>scm:git:git@github.com:xxx/android_agent.git</connection> 
		<url>git@github.com:xxx/android_agent.git</url> </scm> -->

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

	<profiles>
		<profile>
			<id>default-profile</id>
			<activation>
				<activeByDefault>true</activeByDefault>
				<file>
					<exists>${java.home}/../lib/tools.jar</exists>
				</file>
			</activation>
			<properties>
				<toolsjar>${java.home}/../lib/tools.jar</toolsjar>
			</properties>
		</profile>
		
		<profile>
			<id>mac-profile</id>
			<activation>
				<activeByDefault>false</activeByDefault>
				<file>
					<exists>${java.home}/../Classes/classes.jar</exists>
				</file>
			</activation>
			<properties>
				<toolsjar>${java.home}/../Classes/classes.jar</toolsjar>
			</properties>
		</profile>
	</profiles>

	<dependencies>
	    <!-- our rewriter -->
	    <dependency>
			<groupId>groupId</groupId>
			<artifactId>artifactId</artifactId>
			<version>1.0.0</version>
		</dependency>
	    
		<dependency>
			<groupId>org.apache.maven</groupId>
			<artifactId>maven-plugin-api</artifactId>
			<version>2.0</version>
		</dependency>
		
		<dependency>
			<groupId>org.apache.maven.plugin-tools</groupId>
			<artifactId>maven-plugin-annotations</artifactId>
			<version>3.2</version>
			<scope>provided</scope>
		</dependency>
		
		<dependency>
			<groupId>org.codehaus.plexus</groupId>
			<artifactId>plexus-utils</artifactId>
			<version>3.0.8</version>
		</dependency>
		
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.8.2</version>
			<scope>test</scope>
		</dependency>
		
		<dependency>
			<groupId>sun.jdk</groupId>
			<artifactId>tools</artifactId>
			<version>xxx</version>
			<scope>system</scope>
			<systemPath>${toolsjar}</systemPath>
		</dependency>
	</dependencies>

	<build>
	    <finalName>${project.artifactId}-${project.version}</finalName>
		<sourceDirectory>src</sourceDirectory>
		
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-plugin-plugin</artifactId>
				<version>3.2</version>
				<configuration>
					<goalPrefix>agent</goalPrefix>
					<skipErrorNoDescriptorsFound>true</skipErrorNoDescriptorsFound>
				</configuration>
				<executions>
					<execution>
						<id>mojo-descriptor</id>
						<goals>
							<goal>descriptor</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
		</plugins>
		
	</build>
	
</project>

mvn clean install

生成插件并安装到本地库

另外在记录一下:

添加单个jar到maven本地仓库的操作如下: 
1.建立一个新的文件夹,将jar文件存放在该文件夹下。注意文件夹下最好只存放该文件。  
2.在cmd窗口中执行以下命令:  mvn install:install-file -Dfile=<path-to-file> -DgroupId=<group-id> -DartifactId=<artifact-id> -Dversion=<version> -Dpackaging=<packaging> 
例如:
mvn install:install-file  -Dfile=xxx.jar  -DgroupId=xxx  -DartifactId=xxx -Dversion=1.0.0 -Dpackaging=jar




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值