常用maven plugin

Published on 2021-11-25 13:16 in 分类: 随笔 with 狂盗一枝梅
分类: 随笔

1、编译插件

<plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-compiler-plugin</artifactId>
   <version>3.8.1</version>
   <configuration>
      <source>1.8</source>
      <target>1.8</target>
      <encoding>UTF-8</encoding>
   </configuration>
</plugin>

2、单元测试插件

<plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-surefire-plugin</artifactId>
   <version>2.12.4</version>
   <configuration>
      <argLine>-Dfile.encoding=UTF-8</argLine>
      <skip>true</skip>
   </configuration>
</plugin>

3、springboot打包插件

<plugin>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-maven-plugin</artifactId>
   <version>2.4.4</version>
   <configuration>
      <mainClass>com.cosmoplat.Application</mainClass>
   </configuration>
   <executions>
      <execution>
         <goals>
            <goal>repackage</goal>
         </goals>
      </execution>
   </executions>
</plugin>

4、跳过上传jar包

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-deploy-plugin</artifactId>
    <version>2.8.2</version>
    <configuration>
        <skip>true</skip>
    </configuration>
</plugin>

5、创建maven archetype

低于2.4版本的都有bug,不兼容maven高版本
基本命令:mvn clean archetype:create-from-project

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-archetype-plugin</artifactId>
    <version>3.0.0</version>
    <configuration>
        <propertyFile>./archetype.properties</propertyFile>
    </configuration>
</plugin>

但是这个存在一个问题,一些和项目模板源码无关的文件也会被加入模板中,比如IDEA中的.iml文件、.idea目录下的文件。这些“垃圾”文件需要在生成模板时被忽略。 maven-archetype-plugin提供了一个属性配置可以帮助我们实现该能力。在原始项目的根目录(或者你喜欢的其它目录)编写一个.properties文件:

# 原型的坐标 必须声明
archetype.groupId=cn.felord
# 最好按照约定以-archetype结尾
archetype.artifactId=template-archetype
archetype.version=1.0
# 需要忽略的文件夹和文件类型 英文逗号隔开
excludePatterns=**/.idea/**,**/*.iml

这时候执行生成的命令就需要指定该配置文件了:

mvn archetype:create-from-project -Darchetype.properties=./archetype.properties

执行完上述命令之后,会在本地target目录生成项目骨架源码,之后需要将该源码上传到私服: 进入target/generated-sources/archetype目录,执行命令:

mvn clean deploy

将源码推送到私服,注意,如果没有继承bom或者继承了bom文件但是没有在bom文件里定义distributionManagement,则需要在target/generated-sources/archetype/pom.xml文件中添加推送地址。

最后,生成项目

mvn archetype:generate \
 -DgroupId=com.cosmoplat    \
 -DartifactId=jdz-share   \
 -Dversion=1.0-SNAPSHOT       \
 -Dpackage=com.cosmoplat.jdz.share   \
 -DarchetypeGroupId=com.cosmoplat  \
 -DarchetypeArtifactId=cosmoplat-project-template-archetype  \
 -DarchetypeVersion=1.0-SNAPSHOT  \
 -DinteractiveMode=false 

6、通用打可执行jar

如果运行出现Error: A JNI error has occurred, please check your installation and try again错误,加上

注意在此插件上配置

<filters>
                                <filter>
                                    <!-- Do not copy the signatures in the META-INF folder.
                                    Otherwise, this might cause SecurityExceptions when using the JAR. -->
                                    <artifact>*:*</artifact>
                                    <excludes>
                                        <exclude>META-INF/*.SF</exclude>
                                        <exclude>META-INF/*.DSA</exclude>
                                        <exclude>META-INF/*.RSA</exclude>
                                    </excludes>
                                </filter>
                            </filters>
<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.2.4</version>
                <configuration>
                    <createDependencyReducedPom>false</createDependencyReducedPom>
                </configuration>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <filters>
                                <filter>
                                    <!-- Do not copy the signatures in the META-INF folder.
                                    Otherwise, this might cause SecurityExceptions when using the JAR. -->
                                    <artifact>*:*</artifact>
                                    <excludes>
                                        <exclude>META-INF/*.SF</exclude>
                                        <exclude>META-INF/*.DSA</exclude>
                                        <exclude>META-INF/*.RSA</exclude>
                                    </excludes>
                                </filter>
                            </filters>
                            <transformers>
                                <transformer
                                        implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>com.kdyzm.typora.upload.plugin.Main</mainClass>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

7、上传源码

<plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-source-plugin</artifactId>
      <version>3.0.1</version>
      <configuration>
         <attach>true</attach>
      </configuration>
      <executions>
         <execution>
            <phase>compile</phase>
            <goals>
              <goal>jar</goal>
            </goals>
          </execution>
      </executions>
  </plugin>

8、资源过滤

<resources>
	<resource>
		<directory>${project.basedir}</directory>
		<filtering>true</filtering>
		<includes>
			<include>*.properties</include>
			<include>*.xml</include>
			<include>*.yml</include>
			<include>*.yaml</include>
		</includes>
		<excludes>
			<exclude>pom.xml</exclude>
		</excludes>
		<targetPath>${project.build.directory}</targetPath>
	</resource>
	<resource>
		<directory>${basedir}/src/main/resources</directory>
		<excludes>
			<exclude>**/application.yml</exclude>
			<exclude>**/application.properties</exclude>
		</excludes>
	</resource>
</resources>

9、javadoc

<!-- Javadoc -->
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-javadoc-plugin</artifactId>
    <version>2.9.1</version>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>jar</goal>
            </goals>
        </execution>
    </executions>
</plugin>

#maven
目录
复制 复制成功