Maven usage bits and pieces

Project coordinates:

  <groupId>com.alibaba</groupId>
  <artifactId>dubbo-parent</artifactId>
  <version>2.5.4-SNAPSHOT</version>
  <packaging>jar</packaging>

The packaging format of the project is also Maven coordinates , but it is not part
of . A project's groupId:artifactId:version makes it a unique project; you can
not have a project with the same groupId, artifactId, and version identifiers at the same time.

Project dependencies:

<dependencies>
   <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
   </dependency>
   <dependency>
      <groupId>org.apache.geronimo.

      <version>1.0</version>
      <scope>provided</scope>
   </dependency>
</dependencies>

There are three dependency scopes: compile, test, and provided

compile (compile scope): compile is the default scope; if not provided A scope, the scope of the dependency is the compilation scope. Compile-scoped dependencies are available on all classpaths, and they are also packaged.

test (test scope): test scope dependencies are not needed at normal compile and run time, they are only available during the test compile and test run phases.

provided (provided scope): A provided dependency is only used when the JDK or a container has provided the dependency. Scoped dependencies are available on the compile classpath (not runtime). They are not transitive and are not packaged.

Exclude and replace a transitive dependency:

<dependencies>
   <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate</artifactId>
      <version>3.2.5.ga</version>
      <exclusions>
         <exclusion>

            <artifactId>jta</artifactId>
         </exclusion>
      </exclusions>
   </dependency>
   <dependency>
      <groupId>org.apache.geronimo.specs</groupId>
      <artifactId>geronimo-jta_1.1_spec</artifactId>
      < version>1.1</version>
   </dependency>
</dependencies>


[compiler plugin]

<build>
    //The location of the default test source directory and test compilation output directory, by default do not need to set
    <testSourceDirectory>src/test/java</testSourceDirectory >
    <testOutputDirectory>target/test-classes</testOutputDirectory>
    //The default source directory and the location of the compiled output directory, do not need to be set by default
   <sourceDirectory>src/main/java< /sourceDirectory>
   <outputDirectory>target/classes</outputDirectory>
   <plugins>
      <plugin>
         <groupId>org.apache.maven.plugins</groupId> 
         <artifactId>maven-compiler-plugin</artifactId> 
         <version>2.3.2</version> 
         <configuration>
            <source>1.5</source>
            <target>1.5</target>
            <encoding>UTF-8</encoding> 
            <compilerVersion>1.5</compilerVersion> 
            <verbose>true</verbose>
         </configuration>
      </plugin>
   </plugins>
</build>

Java源代码遵循Java 1.5,目标为Java 1.5 JVM       <plugin>    <plugins> <build>

[Ignore unit test failures]




         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-surefire-plugin</artifactId>
         <configuration>
            <testFailureIgnore>true</testFailureIgnore>
         </configuration>
      </plugin>
   </plugins>
</build>

插件参数表达式:

mvn test -Dmaven.test.failure.ignore=true


【跳过单元测试】

<build>
   <plugins>
      <plugin>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-surefire-plugin</artifactId>
         <configuration>
            <skip>true</skip>
         </configuration>
      </plugin>
   </plugins>
</build>

插件参数表达式:

mvn install -Dmaven.test.skip=true

【打包war项目】

<build>
   <plugins>
      <plugin>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-war-plugin</artifactId>
         <version>2.1.1</version>
         <configuration>
            <webResources>
               <resource>
                  <directory>src/main/webapp</directory>
                  <excludes>
                     <exclude>**/*.jpg</exclude>
                  </excludes>
               </resource>
            </webResources>
         </configuration>
      </plugin>
   </plugins>
</build>

【生成可执行jar】

<build>
   <plugins>
      <plugin>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-shade-plugin</artifactId>
         <version>1.4</version>
         <executions>
              <execution>
                  <phase>package</phase>
                  <goals>
                     <goal>shade</goal>
                  </goals>
                  <configuration>
                     <transformers>
                        <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                          <mainClass>com.hainiubl.App</mainClass>
                       </transformer>
                    </transformers>
                 </configuration>
             </execution>
        </ executions>
       </plugin>
   </plugins>
</build>

allows the user to configure the value of Main-Class, and then fill in the value in the /META-INF/MANIFEST.MF file when packaging. Regarding the dependencies of the project, it will decompress all the dependent JAR files, and then merge the obtained .class files together with the .class files of the current project into the final jar package. You can directly execute the jar package through java -jar app.jar

[Make a project distribution package]

<build>
   <plugins>
      <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <configuration>
            <appendAssemblyId>false</appendAssemblyId>
            <descriptors>
                <descriptor>src/main/assemble/assembly.xml</descriptor>
            </descriptors>
        </configuration>
        <executions>
            <execution>
                <id>make-assembly</id>
                <phase>package</phase>
                <goals>
                    <goal>single</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
   </plugins>
</build>

assembly.xml 文件:

<assembly 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/assembly-1.0.0.xsd">
    <id>package</id>
    <formats>
        <format>zip</format>
    </formats>
    <includeBaseDirectory>true</includeBaseDirectory>
    <fileSets>
        <fileSet>
            <directory>src/main/bin</directory>
            <outputDirectory>/</outputDirectory>
        </fileSet>
        <fileSet>
            <directory>src/main/conf</directory>
            <outputDirectory>conf</outputDirectory>
        </fileSet>
    </fileSets>
    <dependencySets>
        <dependencySet>
            <outputDirectory>lib</outputDirectory>
            <scope>runtime</scope>
        </dependencySet>
    </dependencySets>
</assembly>

This distribution may contain the project's Executables, source code, readmes, platform scripts, etc. It supports various mainstream formats such as zip, tar.gz, jar and war, etc. It is highly controllable which files are packaged.
Packaging is expressed using a metadata file called assembly.xml, whose single target can be invoked directly on the command line, or can be bound to the lifecycle.

[Run any local system program]

<build>
   <plugins>
      <plugin>
         <groupId>org.codehaus.mojo</groupId>
         <artifactId>exec-maven-plugin</artifactId>
         <

            <execution>
               <goals>
                  <goal>java</goal>
               </goals>
            </execution>
         </executions>
         <configuration>
            <mainClass>com.hainiubl.App</mainClass>
         </configuration>
      </plugin>
   < /plugins>
</build>

It allows you to run any local system program using mvn exec:exec. In addition to the exec goal, the exec-maven-plugin also provides a java goal that requires you to provide a mainClass parameter, which can then use the current project's dependencies as classpath to run that mainClass in the same JVM. You can configure the relevant running parameters of exec-maven-plugin in the POM, and then run mvn exec:java directly in the command to view the running effect.

[Generate javadoc]

<build>
   <plugins>
      <         
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-javadoc-plugin</artifactId>
         <version>2.7</version>
         <executions>
            <execution>
               <id>attach-javadocs</id>
               <goals>
                  <goal>jar</goal>
               </goals>
            </execution>
         </executions>
      </plugin>   
   </plugins>
</build>

【生成项目源码包】

<build>
   <plugins>
      <plugin>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-source-plugin</artifactId>
         <version>2.1.2</version>
         <executions>
            <execution>
               <id>attach-sources</id>
               <phase>verify</phase>
               <goals>
                  <goal>jar-no-fork</goal>
               < /goals>
            </execution>
         </executions>
      </plugin>
   </plugins>
</build>


Maven default lifecycle phases:

1.validate : Verify that the project is correct and that all necessary information for a complete build is available
2. generate-sources: generate all the source code that needs to be included in the compilation process
3.process-sources: process the source code, such as filtering some values
​​4.generate-resources: generate all the resource files that need to be included in the packaging process
5. process-resources : Copy and process resource files to the target directory, ready to package
6.compile : Compile the source code of the project
7.process-classes: Post-processing files generated by compilation, such as bytecode enhancement of Java classes
8.generate-test-sources: Generate all test sources included in the test compilation process
9.process-test-sources: Process
Test
source
code , such as filtering some values Target Directory
13.test : Run the tests using a suitable unit testing framework. These tests should not require the code to be packaged or released
. 14.prepare-package: Before the actual packaging, perform some operations necessary to prepare the package. This usually produces an expanded, processed version of the
package 15.package: Packages the compiled code into a distributable format, such as a JAR, WAR, or EAR
16.pre-integration-test: performs some integration test runs Action required before.
17.integration-test: If necessary, process the package and publish it to the environment where the integration test can run. 18.post
-integration-test: Perform some actions required after the integration test is run. Such as cleaning the integration test environment.
19.verify: Perform all checks to verify that the package is valid and meets quality specifications
20.install Install the package to the local repository for use by other local projects as dependencies
21.deploy Copy the final package to the remote repository and share it with other developers and projects.


Packaging type related life cycle:

   1.JAR packaging type: JAR is the default packaging type

   life cycle stage target
   process-resources resources:resources
   compile compiler:compile
   process-test-resources resources:testResources
   test-compile compiler:testCompile
   test surefire:test
   package jar:jar
   install install:install
   deploy deploy:deploy

   2.POM packaging type:

   life cycle phase target
   package site:attach-descriptor
   install install:install
   deploy deploy:deploy

   3.WAR packaging type:

   life cycle phase target
   process-resources resources:resources
   compile compiler:compile
   process-test-resources resources:testResources
   test-compile compiler:testCompile
   test surefire:test
   package war:war
   install install:install
   deploy deploy:deploy

   4. EAR package type:

   life cycle stage target
   generate-resources ear:generate-application-xml
   process-resources resources:resources
   package ear:ear
   install install:install
   deploy                  deploy:deploy





Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326128951&siteId=291194637