一、官方插件
1、常用的官方插件仓库
https://maven.apache.org/plugins/ 和 http://www.mojohaus.org/plugins.html
2、findbugs 静态代码检查的插件(基于字节码进行检查) 这个还是比较好用的,只需要在项目中引入下面的插件,并执行 mvn findbugs:findbugs 即可
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>findbugs-maven-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<threshold>High</threshold>
<effort>Default</effort>
<findbugsXmlOutput>true</findbugsXmlOutput>
<findbugsXmlOutputDirectory>target/site</findbugsXmlOutputDirectory>
</configuration>
</plugin>
3、resource 打包源代码
<!-- resource插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.7</version>
<executions>
<execution>
<id>attach-source</id>
<phase>install</phase>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<archive>
<manifest>
<mainClass>com.kevin.Test</mainClass>
</manifest>
</archive>
<descriptorRefs>
<!-- 将多层依赖的包一起打进来 -->
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
<!-- 打包jar文件时,配置manifest文件,加入lib包的jar依赖 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
<!-- <executions>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classesDirectory>${project.outputDirectory}</classesDirectory>
<finalName>jeesite</finalName>
<outputDirectory>${project.build.directory}/${project.artifactId}/WEB-INF/lib</outputDirectory>
<includes>
<include>com/thinkgem/jeesite/**</include>
</includes>
</configuration>
</execution>
</executions> -->
</plugin>
5、wvengen 代码混淆
<!-- 混淆代码
<plugin>
<groupId>com.github.wvengen</groupId>
<artifactId>proguard-maven-plugin</artifactId>
<version>2.0.11</version>
<executions>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>proguard</goal>
</goals>
</execution>
</executions>
<configuration>
<obfuscate>true</obfuscate>
<options>
<option>-injars ${project.build.directory}/${project.artifactId}/WEB-INF/lib/jeesite.jar</option>
</options>
<outjar>${project.artifactId}/WEB-INF/lib/jeesite_out.jar</outjar>
<outputDirectory>${project.build.directory}</outputDirectory>
<proguardInclude>${basedir}/proguard.cfg</proguardInclude>
<source>${jdk.version}</source>
<target>${jdk.version}</target>
<encoding>${project.build.sourceEncoding}</encoding>
<libs>
<lib>${java.home}/lib/rt.jar</lib>
<lib>${java.home}/lib/jsse.jar</lib>
<lib>${java.home}/lib/jce.jar</lib>
</libs>
<addMavenDescriptor>false</addMavenDescriptor>
</configuration>
<dependencies>
<dependency>
<groupId>net.sf.proguard</groupId>
<artifactId>proguard-base</artifactId>
<version>4.9</version>
<scope>runtime</scope>
</dependency>
</dependencies>
</plugin> -->
6、tomcat 6和7
<!-- tomcat6插件 -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat6-maven-plugin</artifactId>
<version>${tomcat.version}</version>
<configuration>
<port>${webserver.port}</port>
<path>/${project.artifactId}</path>
<uriEncoding>${project.build.sourceEncoding}</uriEncoding>
</configuration>
</plugin>
<!-- tomcat7插件 -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>${tomcat.version}</version>
<configuration>
<port>${webserver.port}</port>
<path>/${project.artifactId}</path>
<uriEncoding>${project.build.sourceEncoding}</uriEncoding>
</configuration>
</plugin>
二、自定义插件
1、新建一个项目并且<packaging>maven-plugin</packaging>
2、添加依赖
<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>3.5.0</version>
</dependency>
<dependency>
<groupId>org.apache.maven.plugin-tools</groupId>
<artifactId>maven-plugin-annotations</artifactId>
<version>3.5</version>
<scope>provided</scope>
</dependency>
</dependencies>
3、写一个类,使用注解指定名称属性(插件的goal)和阶段(phase),继承自AbstractMojo并且实现未实现的方法的业务逻辑。
将插件项目mvn clean install 成功后再将引入插件的项目 mvn install -Dname=12345 则会打印出插件的信息
<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>com.kevin.plugin</groupId>
<artifactId>kevin</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>maven-plugin</packaging>
<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>3.5.0</version>
</dependency>
<dependency>
<groupId>org.apache.maven.plugin-tools</groupId>
<artifactId>maven-plugin-annotations</artifactId>
<version>3.5</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
package com.kevin.plugin;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
@Mojo(name="kevin", defaultPhase = LifecyclePhase.PACKAGE)
public class KevinMojo extends AbstractMojo{
public void execute() throws MojoExecutionException, MojoFailureException {
// TODO Auto-generated method stub
System.out.println("kevin的maven插件,运行起来吧!");
}
}
<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>com.kevin.plugin</groupId>
<artifactId>testplugin</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>com.kevin.plugin</groupId>
<artifactId>kevin</artifactId>
<version>0.0.1-SNAPSHOT</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>kevin</goal>
</goals>
</execution>
</executions>
<configuration>
<name>kevin123</name>
</configuration>
</plugin>
</plugins>
</build>
</project>
三、私服仓库
1、下载 地址:https://www.sonatype.com/download-oss-sonatype
2、解压、(安装),并进行启动,默认用户名密码为 admin/admin123
这就是一个web项目,可以看见public repositories 中可以看看见,snapshots为对应的项目的仓库,3rd party为第三方仓库(如我们需要引入阿里的oss,cdn,或短信的 三方私服),releases 为对应的项目的仓库。其中,仓库可能需要区分公司的部门或项目组,以及用户、角色、权限等。也可以为每一个仓库设置定时任务去更新。
3、修改setting中的配置信息,mirror或者profile中设置私服的地址,将项目与私服仓库进行关联。
<distributionManagement>
<repository>
<!-- id必须与私服中的一致 name可以只作为区分 -->
<id>kevin-releases</id>
<name>nexus release repository</name>
<url>http://192.168.0.1:8081/nexus/content/repositories/releases/</url>
</repository>
<snapshotRepository>
<!-- id必须与私服中的一致 name可以只作为区分 -->
<id>kevin-snapshots</id>
<name>nexus snapshots repository</name>
<url>http://192.168.0.1:8081/nexus/content/repositories/snapshots/</url>
</snapshotRepository>
</distributionManagement>
<!-- 服务的账号密码设置 -->
<servers>
<server>
<id>kevin-releases</id>
<username>admin</username>
<password>admin123</password>
</server>
<server>
<id>kevin-snapshots</id>
<username>admin</username>
<password>admin123</password>
</server>
</servers>
四、maven项目的模板化(archetype)
可以直接使用命令,或者可以在eclipse等ide中创建模板,也可以将该模板发送到私服仓库方便公司项目组使用
1、生成archetype(cd 到项目下)
a)mvn archetype:creat-from-project
b)cd /target/generated-sources/archetype
c)mvn install
2、使用生成的模板
可以将其模板添加到ide中,创建maven项目时,直接选择也可以使用命令进行创建 mvn archetype:generate -DarchetypeCatalog=local
后续需要输入项目的 groupId artifactId version等信息即可。