maven common plug-ins

Keywords: Maven JDK Java Scala

1, Maven compiler plugin

1. It is used to set the jdk version used when maven is packaged. maven is a java framework, so it is only for jdk. scala needs to set it separately.

2.usage

2.1. Setting plug-ins

<plugin>                                                                                                      
    <! -- specifies the JDK version compiled by maven. If not specified, maven 3 defaults to JDK 1.5 maven 2 defaults to jdk1.3 -- >                                                                 
    <groupId>org.apache.maven.plugins</groupId>                                                                                               
    <artifactId>maven-compiler-plugin</artifactId>                                                                         
    <version>3.1</version>                                                                                                                    
    <configuration>                                                                                                                           
        <! -- generally speaking, target and source are the same, but sometimes in order to make the program run in other versions of jdk (for the lower version of target jdk, the syntax not supported in the lower version of jdk cannot be used in the source code), there will be a case where target is different from source -- >                    
        < source > 1.8 < / source > <! -- JDK version used by source code -- >                                                                                             
        < target > 1.8 < / target > <! -- compiled version of the target class file to be generated -- >                                                                                     
        < encoding > UTF-8 < / encoding > <! -- character set encoding -- >
        < skiptests > true < / skiptests > <! -- skip tests -- >                                                                             
        <verbose>true</verbose>
        <showWarnings>true</showWarnings>                                                                                                               
        < fork > true < / fork > <! -- for the compilerVersion tag to take effect, you also need to set fork to true to explicitly indicate the available configuration of the compiled version -- >                                                        
        < executable > <! -- path to javac -- >           
        < compilerversion > 1.3 < / compilerversion > <! -- specifies the version of the compiler that the plug-in will use -- >                                                                         
        < meminitial > 128M < / meminitial > <! -- initial memory used by compiler -- >                                                                                      
        < maxmem > 512M < / maxmem > <! -- maximum memory used by compiler -- >                                                                                              
        < compilerargument > - verbose - bootclasspath ${Java. Home} \ lib \ rt.jar < / compilerargument > <! -- this option is used to pass parameter options that the compiler does not contain but supports -- >               
    </configuration>                                                                                                                          
</plugin>

2.2. In addition to setting plugin in build, you can also directly set plugin in

<properties>
    <! -- Maven compiler plugin will compile the source code with the specified version of JDK (for the compile run environment) - >
    <maven.compiler.source>1.8</maven.compiler.source>
	<! -- Maven compiler plugin will use the specified version of JDK to compile java files into class files (for the compile run environment) - >
    <maven.compiler.target>1.8</maven.compiler.target>
<properties>

2.3.SBoot project can be implemented through java.version

<properties>
	<java.version>1.8</java.version>
</properties>

2.4. Global settings. The above three settings will only take effect on the project and subproject of pom, such as global settings. To change setting.xml

<profile>
    <!-- Defined compiler plug-ins ID,Global uniqueness -->
    <id>jdk-1.7</id>
    <!-- Plug in tag, activeByDefault Is it the default Compiler, jdk Provide compiler version -->
    <activation>
        <activeByDefault>true</activeByDefault>
        <jdk>1.7</jdk>
    </activation>
    <!-- configuration information source-Source information, target-Bytecode information, compilerVersion-Build process version -->
    <properties>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>                                							<maven.compiler.compilerVersion>1.7</maven.compiler.compilerVersion>
    </properties>
</profile>

2, Resource copy plug in

1.Maven only packages the configuration files in src/main/resources and src/main/java to jar by default. For example, mapper.xml of mybatis, which needs to be packaged to the configuration files in the classpath, it needs special processing. Either put XxxMapper.xml in resources or specify the packaging directory. Although plug-in, resource / resource is not used for plugin

2.usage

<build>
    <resources>
        <resource>
            <directory>src/main/java</directory>
            <includes>
                <include>**/*.xml</include>
            </includes>
        </resource>
        <resource>
            <directory>src/main/resources</directory>
            <includes>
                <include>**/*.xml</include>
                <include>**/*.properties</include>
            </includes>
        </resource>
    </resources>
</build>

2.1. * * indicates the package at any level of any package, including indicates the source, and directory indicates the destination

3, Maven enforcer plugin

1. Used to set validation rules when packaging. Specify the KV to check and configure the policy. For example, if the jdk is specified as 1.8, if it is not 1.8, it will be ignored or failed.

2.usage

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-enforcer-plugin</artifactId>
    <version>1.4.1</version>
    <executions>
        <execution>
            <goals>
                <goal>enforce</goal>
            </goals>
            <configuration>
                <rules>
                    <!--1 Items to be verified correspond to 1 label, for example jdk Version is requireJavaVersion Label-->
                    <requireJavaVersion>
                        <version>1.8</version>
                    </requireJavaVersion>
                    <requireMavenVersion>
                        <version>3.3</version>
                    </requireMavenVersion>
                </rules>
            </configuration>
        </execution>
    </executions>
</plugin>

2.1. Checked items: http://maven.apache.org/enforcer/enforcer-rules/index.html

{1} Whether to check delivery dependency
{2} JDK version
{3} Maven version
{4} Jar version conflict check
<dependencyConvergence>
   <uniqueVersion>false</uniqueVersion>
</dependencyConvergence>

4, Maven source plugin

1. By default, the source jar will be automatically generated under target

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-source-plugin</artifactId>
    <version>3.0.1</version>
    <executions>
        <execution>
            <id>attach-sources</id>
            <goals>
                <goal>jar</goal>
            </goals>
        </execution>
    </executions>
</plugin>

5, Maven Javadoc plugin

1. The jar of doc will be automatically generated under target. Only the documents corresponding to the generated java files can be extracted, and scala files cannot be extracted after testing

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-javadoc-plugin</artifactId>
    <version>3.0.1</version>
    <configuration>
        <javadocVersion>1.8</javadocVersion>
    </configuration>
    <executions>
        <execution>
            <id>attach-javadocs</id>
            <goals>
                <goal>jar</goal>
            </goals>
        </execution>
    </executions>
</plugin>

6, Maven release plugin

1. Version management tools

7, Maven shade plugin

1. Different jars in the project depend on different versions of the same jar, and strictly correspond to each other. exclude cannot be used. Class loader will load the first dependency encountered by default. When multiple versions of the same jar coexist, the same version will be relied on by default. At this time, we need to use the shade plugin to put the priority high into the shadow, so as to realize the purpose of going back to each home

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>3.2.1</version>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
            <configuration>
                <finalName>${project.artifactId}-shade-${project.version}</finalName>
                <filters>
                    <filter>
                        <artifact>*:*</artifact>
                        <excludes>
                            <exclude>META-INF/*.SF</exclude>
                            <exclude>META-INF/*.DSA</exclude>
                            <exclude>META-INF/*.RSA</exclude>
                        </excludes>
                    </filter>
                </filters>
                <relocations>
                    <relocation>
                        <pattern>com.google</pattern>
                        <shadedPattern>com.shade.google</shadedPattern>
                    </relocation>
                    <relocation>
                        <pattern>org.jpmml</pattern>
                        <shadedPattern>org.shade.jpmml</shadedPattern>
                    </relocation>
                    <relocation>
                        <pattern>org.dmg</pattern>
                        <shadedPattern>org.shade.dmg</shadedPattern>
                    </relocation>
                </relocations>
            </configuration>
        </execution>
    </executions>
</plugin>

8, Maven Scala plugin

1. It is used to compile scala files into class files and type them into the classpath of jar

<plugin>
    <!--scala Primordial sbt(Similar java maven)Do development,Now you can use this plug-in to maven Development in progress-->
    <groupId>org.scala-tools</groupId>
    <artifactId>maven-scala-plugin</artifactId>
    <version>2.15.2</version>
    <executions>
        <execution>
            <id>scala-compile-first</id>
            <goals>
                <goal>compile</goal>
            </goals>
            <configuration>
                <includes>
                    <!--<include>Used to set the source-->
                    <include>**/*.scala</include>
                </includes>
            </configuration>
        </execution>
    </executions>
</plugin>

9, Maven assembly plugin

1. Jar used to generate full jar and class file only jar

2.usage

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <configuration>
        <appendAssemblyId>true</appendAssemblyId>
        <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
        <archive>
            <manifest>
                <mainClass>cn.dc.SparkWithPMMLAndCluster</mainClass>
            </manifest>
        </archive>
    </configuration>
    <executions>
        <execution>
            <id>make-assembly</id>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
        </execution>
    </executions>
</plugin>

10, Maven dependency plugin

1. It is used to copy jar, copy dependency to specified directory, and ${project.build.directory} represents the classpath of jar

<!-- Copy project dependency package to<outputDirectory>Specified directory -->
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
        <execution>
            <id>copy-dependencies</id>
            <phase>package</phase>
            <goals>
                <goal>copy-dependencies</goal>
            </goals>
            <configuration>
                <outputDirectory>
                    ${project.build.directory}/lib
                </outputDirectory>
            </configuration>
        </execution>
    </executions>
</plugin>

11, Maven jar plugin

<!-- The path of the project dependency package (compared with the<outputDirectory>Corresponding) add to classPath in -->
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <configuration>
        <archive>
            <manifest>
                <addClasspath>true</addClasspath>
                <classpathPrefix>lib/</classpathPrefix>
                <mainClass>cn.dc.SparkWithPMMLAndCluster</mainClass>
            </manifest>
            <manifestEntries>
                <Class-Path>./</Class-Path>
            </manifestEntries>
        </archive>
    </configuration>
</plugin>
Published 15 original articles, won 0 praise and 371 visitors
Private letter follow

Posted by Rohan Shenoy on Sun, 09 Feb 2020 00:46:26 -0800