Maven的几个常用plugin

桃扇骨 2022-02-03 09:27 405阅读 0赞

编译Java源码,一般只需设置编译的jdk版本

复制代码

  1. <plugin>
  2. <groupId>org.apache.maven.plugins</groupId>
  3. <artifactId>maven-compiler-plugin</artifactId>
  4. <version>3.6.0</version>
  5. <configuration>
  6. <source>1.8</source>
  7. <target>1.8</target>
  8. </configuration>
  9. </plugin>

复制代码

或者在properties设置jdk版本

  1. <properties>
  2. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  3. <maven.compiler.source>1.8</maven.compiler.source>
  4. <maven.compiler.target>1.8</maven.compiler.target>
  5. </properties>

maven-dependency-plugin

用于复制依赖的jar包到指定的文件夹里

复制代码

  1. <plugin>
  2. <groupId>org.apache.maven.plugins</groupId>
  3. <artifactId>maven-dependency-plugin</artifactId>
  4. <version>2.10</version>
  5. <executions>
  6. <execution>
  7. <id>copy-dependencies</id>
  8. <phase>package</phase>
  9. <goals>
  10. <goal>copy-dependencies</goal>
  11. </goals>
  12. <configuration>
  13. <outputDirectory>${project.build.directory}/lib</outputDirectory>
  14. </configuration>
  15. </execution>
  16. </executions>
  17. </plugin>

复制代码

maven-jar-plugin

打成jar时,设定manifest的参数,比如指定运行的Main class,还有依赖的jar包,加入classpath中

复制代码

  1. <plugin>
  2. <groupId>org.apache.maven.plugins</groupId>
  3. <artifactId>maven-jar-plugin</artifactId>
  4. <version>2.4</version>
  5. <configuration>
  6. <archive>
  7. <manifest>
  8. <addClasspath>true</addClasspath>
  9. <classpathPrefix>/data/lib</classpathPrefix>
  10. <mainClass>com.zhang.spring.App</mainClass>
  11. </manifest>
  12. </archive>
  13. </configuration>
  14. </plugin>

复制代码

maven-antrun-plugin

在maven中运行Ant任务,比如在打包阶段,对文件进行复制

复制代码

  1. <plugin>
  2. <groupId>org.apache.maven.plugins</groupId>
  3. <artifactId>maven-antrun-plugin</artifactId>
  4. <version>1.7</version>
  5. <executions>
  6. <execution>
  7. <phase>package</phase>
  8. <goals>
  9. <goal>run</goal>
  10. </goals>
  11. <configuration>
  12. <target name="copy">
  13. <delete>
  14. <fileset dir="target" includes="*.properties"></fileset>
  15. </delete>
  16. <copy todir="target">
  17. <fileset dir="files"></fileset>
  18. </copy>
  19. </target>
  20. </configuration>
  21. </execution>
  22. </executions>
  23. </plugin>

复制代码

wagon-maven-plugin

用于一键部署,把本地打包的jar文件,上传到远程服务器上,并执行服务器上的shell命令

复制代码

  1. <plugin>
  2. <groupId>org.codehaus.mojo</groupId>
  3. <artifactId>wagon-maven-plugin</artifactId>
  4. <version>1.0</version>
  5. <configuration>
  6. <serverId>crawler</serverId>
  7. <fromDir>target</fromDir>
  8. <includes>*.jar,*.properties,*.sh</includes>
  9. <url>sftp://59.110.162.178/home/zhangxianhe</url>
  10. <commands>
  11. <command>chmod 755 /home/zhangxianhe/update.sh</command>
  12. <command>/home/zhangxianhe/update.sh</command>
  13. </commands>
  14. <displayCommandOutputs>true</displayCommandOutputs>
  15. </configuration>
  16. </plugin>

复制代码

tomcat7-maven-plugin

用于远程部署Java Web项目

复制代码

  1. <plugin>
  2. <groupId>org.apache.tomcat.maven</groupId>
  3. <artifactId>tomcat7-maven-plugin</artifactId>
  4. <version>2.2</version>
  5. <configuration>
  6. <url>http://59.110.162.178:8080/manager/text</url>
  7. <username>linjinbin</username>
  8. <password>linjinbin</password>
  9. </configuration>
  10. </plugin>

复制代码

maven-shade-plugin

用于把多个jar包,打成1个jar包

一般Java项目都会依赖其他第三方jar包,最终打包时,希望把其他jar包包含在一个jar包里

复制代码

  1. <plugin>
  2. <groupId>org.apache.maven.plugins</groupId>
  3. <artifactId>maven-shade-plugin</artifactId>
  4. <version>2.4.3</version>
  5. <executions>
  6. <execution>
  7. <phase>package</phase>
  8. <goals>
  9. <goal>shade</goal>
  10. </goals>
  11. <configuration>
  12. <transformers>
  13. <transformer
  14. implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
  15. <manifestEntries>
  16. <Main-Class>com.meiyou.topword.App</Main-Class>
  17. <X-Compile-Source-JDK>${maven.compile.source}</X-Compile-Source-JDK>
  18. <X-Compile-Target-JDK>${maven.compile.target}</X-Compile-Target-JDK>
  19. </manifestEntries>
  20. </transformer>
  21. </transformers>
  22. </configuration>
  23. </execution>
  24. </executions>
  25. </plugin>

复制代码

发表评论

表情:
评论列表 (有 0 条评论,405人围观)

还没有评论,来说两句吧...

相关阅读