maven's usage record

Maven is a project management and project description tool; it can make project management simpler and more standardized;
Its construction life cycle includes: preparing resources - Validation - Compilation - testing - Packaging - Installation - Publishing

maven has three build profiles
1. Each project has the configuration file pom.xml.
2. Each user has the configuration file% USER_HOME%/.m2/settings.xml, such as cat ~/.m2/settings.xml for current users to view the configuration content;
3. There is also a global configuration file% M2_HOME%/conf/settings.xml. For example, Maven installed with brew in mac can view the configuration content through cat/usr/local/Cellar/maven/3.5.4/libexec/conf/settings.xml.

(1)mac installation maven

brew search maven
brew install maven
mvn -v 

(2) Generate a local java project called testweb

mvn archetype:generate -DgroupId=www.testweb.webapp -DartifactId=testweb -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false
#Archtype is a built-in plug-in for maven
#DgroupId package name
#DartifactId project name
#The values commonly used in Darchetype ArtifactId project generation structures are
#   Maven-archetype-quickstart Java Project project structure
#   Maven-archetype-webapp Java Web Project project structure

When the above command is executed, the following directory structure is generated
testweb
├── pom.xml
└── src
└── main
├── resources
└── webapp
├── WEB-INF
│   └── web.xml
└── index.jsp
The pom.xml (pom:project object model) contains project construction information. The content of pom.xml can be as follows:

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <!--Specify package name-->
  <groupId>www.testweb.webapp</groupId>
  <!--Specify project name-->
  <artifactId>testweb</artifactId>
  <!--Specify the final packaging-->
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>testweb Maven Webapp</name>
  <url>http://maven.apache.org</url>

  <!--In need of upload jar Project-time allocation of resources distributionManagement,among<id>Settings need to follow.m2 Under the folder settings.xml in<servers>Next id Same.<url>Need to specify nexus Configured in hosts Repository The address of the resource; this is configured maven Compile the project and pass it mvn deploy To release jar Mirror servers within resources-->
  <distributionManagement>
        <repository>
            <id>server_id</id>
            <name>myNexus</name>
            <url>http://nexus_ip:8081/repository/host-releases/</url>
        </repository>
  </distributionManagement>

  <!--Specify external dependencies-->
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
         <!--Externally dependent package names-->
         <groupId>myjdk</groupId>
         <!--Externally dependent project names-->
         <artifactId>myjdk</artifactId>
         <!--Specifies the scope of use of this external dependency, with optional values of compile,provided,provided,test,system,import-->
         <!--Specific reference http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html-->
         <scope>system</scope>
         <version>1.0</version>
         <!--Specify dependent package paths-->
         <systemPath>${basedir}\src\lib\myjdk.jar</systemPath>
     </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-antrun-plugin</artifactId>
        <version>1.1</version>
        <executions>
          <execution>
            <id>id.pre-clean</id>
            <phase>pre-clean</phase>
            <goals>
              <goal>run</goal>
            </goals>
            <configuration>
              <tasks>
                <echo>pre-clean phase</echo>
              </tasks>
            </configuration>
          </execution>

          <execution>
            <id>id.clean</id>
            <phase>clean</phase>
            <goals>
              <goal>run</goal>
            </goals>
            <configuration>
              <tasks>
                <echo>clean phase</echo>
              </tasks>
            </configuration>
          </execution>

          <execution>
            <id>id.post-clean</id>
            <phase>post-clean</phase>
            <goals>
              <goal>run</goal>
            </goals>
            <configuration>
              <tasks>
                <echo>post-clean phase</echo>
              </tasks>
            </configuration>
          </execution>
          <execution>
            <id>id.validate</id>
            <phase>validate</phase>
            <goals>
              <goal>run</goal>
            </goals>
            <configuration>
              <tasks>
                <echo>validate phase</echo>
              </tasks>
            </configuration>
          </execution>

          <execution>
            <id>id.compile</id>
            <phase>compile</phase>
            <goals>
              <goal>run</goal>
            </goals>
            <configuration>
              <tasks>
                <echo>compile phase</echo>
              </tasks>
            </configuration>
          </execution>

          <execution>
            <id>id.test</id>
            <phase>test</phase>
            <goals>
              <goal>run</goal>
            </goals>
            <configuration>
              <tasks>
                <echo>test phase</echo>
              </tasks>
            </configuration>
          </execution>

          <execution>
            <id>id.package</id>
            <phase>package</phase>
            <goals>
              <goal>run</goal>
            </goals>
            <configuration>
              <tasks>
                <echo>package phase</echo>
              </tasks>
            </configuration>
          </execution>

          <execution>
            <id>id.deploy</id>
            <phase>deploy</phase>
            <goals>
              <goal>run</goal>
            </goals>
            <configuration>
              <tasks>
                <echo>deploy phase</echo>
              </tasks>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
    <finalName>testweb</finalName>
  </build>

  <!--profiles Define variable configurations for each environment-->
  <profiles>

    <profile>
    <!-- Local Development Environment -->
      <id>dev</id>
      <!--properties Some properties defined by oneself-->
      <properties>

      </properties>
    </profile>
    <profile>
      <activation>
        <activeByDefault>true</activeByDefault>
      </activation>
      <id>prod</id>
      <!-- Publishing environment -->
      <properties>

      </properties>
    </profile>
  </profiles> 
</project>
1. Where dependencies specify the dependent jar package to be included, packaging is the way to package when the departure is specified, such as jar, war, ear, etc.
2. groupId:artifactId:version in the base labels a project in maven's warehouse
 3. Executing the mvn help:effective-pom command in the project directory allows you to view the detailed POM information of the project.
4. build is the configuration of the construction process. An insert, org.apache.maven.plugins:maven-antrun-plugin:1.1, is introduced to perform an echo task in the three stages of pre-clean, clean and post-clean of the construction process. validate, compile, test, package and deploy also perform an echo in each of these stages.
5. maven has profile function, which sets different configurations for the project. For example, the project has two configurations: Dev and prod, i.e. development environment configuration and production environment configuration. The prod is set to the default configuration environment by activeByDefault, that is, when mvn package is invoked, the configuration environment information of prod is invoked. If the configuration environment information of dev needs to be invoked, the mvn package-P dev is needed, that is to say, the configuration environment used needs to be referred to by the parameter-P; the files corresponding to these configurations are placed in the src/main/resources directory. Under the config folder, there are multiple environment configuration files under config. The naming rule is application-environment name.properties.

(3)

pre-clean specifies what to do before compilation is cleared
Files and directories generated during compilation prior to mvn clean cleaning
mvn post-clean has three stages: MVN pre-clean, MVN clean, mvn post-clean; that is, specifying what to do after compilation is cleared
mvn validate verifies that the project is correct and that the information required is useful?
MVN compile compile project
mvn test test test project
mvn package packaging projects such as generating war packages or jar packages
MVN deployment layout project

Tags: Java Maven xml Apache brew

Posted on Thu, 31 Jan 2019 12:09:16 -0800 by system_critical