Maven environment construction and 5-minute experience

Environment construction (only under linux, the same is true under windows, but the configuration of environment variables is different)

  • First go to the official website of maven to download the corresponding binary .tar.gz package, and extract the corresponding package to the directory you specify
  • Configure environment variables and add the path after the last line of the /etc/profile file, for example, mine is:
#maven path configuration by eric
export PATH=/home/eric/software/apache-maven-3.5.0/bin/:$PATH

You just need to replace the path with your path

  • Exit the shell and log in again to take effect
  • Enter mvn -v in the shell, if a message similar to the following pops up, the configuration is successful
Apache Maven 3.0.5 (r01de14724cdef164cd33c7c8c2fe155faf9602da; 2013-02-19 14:51:28+0100)
Maven home: D:\apache-maven-3.0.5\bin\..
Java version: 1.6.0_25, vendor: Sun Microsystems Inc.
Java home: C:\Program Files\Java\jdk1.6.0_25\jre
Default locale: nl_NL, platform encoding: Cp1252
OS name: "windows 7", version: "6.1", arch: "amd64", family: "windows"

Maven in 5 Minutes

The following content comes from the translation of maven's official website, only the important ones are translated, and the irrelevant content is skipped. But each step has been run personally (it's better not to use JDK9, because some new features of JDK9 will cause runtime errors, these errors can also be corrected, but as a novice, don't be hard on yourself).

Install

I've already said it, so I won't repeat it here

Create a project

You need a directory to store your project, you can choose to create a new directory or open a shell in that directory. Execute the following maven goal in your command line:

mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

If you just installed maven, this may take a while (actually a long time). This is because maven is downloading the latest modules (plugin jars and other files) into your local repository. You may have to execute this command many times before it finally succeeds. This is because the remote server may time out before you fully download the number, so the download will fail, but don't worry, it will always succeed.

You will notice that the generate goal creates a directory with the same artifactId, switch into that directory.

cd my-app

In this directory you will find the following directory structure, the standard directory structure

my-app
|-- pom.xml
`-- src
    |-- main
    |   `-- java
    |       `-- com
    |           `-- mycompany
    |               `-- app
    |                   `-- App.java
    `-- test
        `-- java
            `-- com
                `-- mycompany
                    `-- app
                        `-- AppTest.java

The src/main/java directory contains the source files of the project, src/test/java contains the test source files, pom.xml is the Project Object Model file of the project (the core configuration file of maven), POM

The POM

pom.xml is the core of maven's project configuration file. It is a separate file that contains the main information for compiling the project, which can be specified by you. A POM is a huge and frighteningly complex configuration file, but you don't need to know all the details to use it effectively. This project POM is 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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.mycompany.app</groupId>
  <artifactId>my-app</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>Maven Quick Start Archetype</name>
  <url>http://maven.apache.org</url>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.8.2</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

What did you just do?

You executed the maven goal archetype:generate and passed a few parameters. The prefix archetype is the plugin that contains the goal . If you are familiar with Ant , you can treat this as a task. This goal creates a simple project based on the archetype. It is now possible to say that a plugin is a collection of common purpose goals. For example jboss-maven-plugin, its purpose is to handle various jboss components

compile the project

mvn package

This command will print out some events and end up like this

 ...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2 seconds
[INFO] Finished at: Thu Jul 07 21:34:52 CEST 2011
[INFO] Final Memory: 3M/6M
[INFO] ------------------------------------------------------------------------

You may have noticed that the second command is a separate command package, different from the first. This command is not a goal, it's a phase. A phase is a step in the compilation lifecycle , which is an ordered sequence of phases. When a phase is given, maven will execute each phase until it contains the given phase. For example, if we execute the compile phase, the executed phase is actually as follows:

  1. validate
  2. generate-sources
  3. process-sources
  4. generate-resources
  5. process-resources
  6. compile

You can test the newly compiled JAR with the following command

java -cp target/my-app-1.0-SNAPSHOT.jar com.mycompany.app.App

will print out

Hello World!

Run Maven Tools

maven Phases

Although there is rarely a comprehensive list, here are some of the most commonly used phases of the execution lifecycle

  • validate: Verify that the project is correct and that all necessary information is available.
  • compile: compile the source files of the project
  • test: Test the compiled source files using a suitable testing framework. These tests do not require the source code to be packaged or deployed
  • package: Get compiled source files and package them in a release format, such as JAR
  • integration-test: if necessary, execute and deploy this package to an environment where integration tests can be executed
  • verify: run all checks to ensure that the package is valid and also meets quality standards
  • install: Install this package to the local repository as a dependency of other local repositories
  • deploy: Make an integration or release environment, copy the final package to the remote repository and share it with other developers or projects
    . There are two maven declaration cycles that are not in the default list above, namely:
  • clean: clean up the files generated by the previous compilation process
  • site: Generate documentation
    Phases for this project are usually related to goal. The phase executed by the explicit goal is dependent on the type of the project's package. For example, the package executes jar:jar if the project is of type JAR, and executes war:war if the project is of type WAR.
    An interesting note to mention is that phases and goals can be executed in a certain order.
mvn clean dependency:copy-dependencies package

This command will clear the project, copy the dependencies, and package the project (and of course execute all phases up to the package).

Generating the Site

mvn site

This phase will generate a document that depends on the project's POM configuration. You can consult the documentation generated under target/site.

in conclusion

We hope this sneak peek sparked your interest in exploring maven's rich functionality. Remember that this is a brief snapshot. Next, you need to know more details in detail, please visit the maven tutorial

Guess you like

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