Maven插件开发

来源:互联网 发布:网络贷款征信查询平台 编辑:程序博客网 时间:2024/06/05 17:44

首先使用命令创建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
原创粉丝点击