Maven installation
Before installing Maven, make sure that jdk is installed and the environment variable JAVA_HOME is configured. The specific installation steps are as follows:
1. Download the compressed package of the maven project from apache. The download address is: http://maven.apache.org/download.html. For example, the latest Maven version is 3.0.4, then the installation file I downloaded is apache-maven-3.0.4.zip.
2. Unzip the downloaded compressed package to the installation directory of Maven, for example, D://develop, then after decompression, it is D://develop//apache-maven-3.0.4.
3. Add the environment variable M2_HOME, whose value is Maven's home directory, such as D://develop//apache-maven-3.0.4.
4. Add the environment variable M2, and its value will be the bin directory of the maven installation directory, that is, D://develop//apache-maven-3.0.4//bin. %M2_HOME%//bin can also be used for windows systems, and $M2_HOME/bin can also be used for linux systems.
5. Add the environment variable M2 to the PATH variable. For Windows systems, you can add ";%M2%" to the value of the PATH variable. For Linux systems, you can use "export path=$path:$M2"
6. There is also an optional environment variable MAVEN_OPTS. This environment variable is mainly used to configure Maven to specify JVM properties when using jdk. If specified, its value is "-Xms256m -Xmx512m".
After the above steps, Maven was successfully installed. Next, we can use mvn --version in the command window to verify whether Maven is installed successfully. If the installation version of Maven can be output correctly, it means that it has been installed successfully.
After we create a simple Maven project (just configure the dependency in pom.xml), run mvn clean install to build the project. We do not need to manually download any jars. This depends entirely on the existence of the central repository, which will automatically download from the repository. The definition of this repository is in ${M2_HOME}/lib/maven-2.0.10-uber.jar. You can find the /org/apache/maven/project/pom-4.0.0.xml file in which the address of the default central repository is defined:
<repositories> <repository> <id> central</id> <name> Maven Repository Switchboard</name> <layout> default</layout> <url> http://repo1.maven.org/maven2</url> <snapshots> <enabled> false</enabled> </snapshots> </repository> </repository> </repository>
If you want to override the default address of the central repository, then we will use the image here, and we will also configure it in setting.xml:
<settings> … <mirrors> <mirror> <id> maven-net-cn</id> <name> Maven China Mirror</name> <url> http://maven.net.cn/content/groups/public/</url> <mirrorOf> central</mirrorOf> </mirror> </mirror> … </settings>
, means that only mirroring for central warehouses. If you want to mirror all warehouses, you can change it to: *
Detailed explanation of Maven configuration
Compared with maven1, maven2 requires much less configuration files, mainly concentrated in pom.xml and settings.xml.
Let’s first talk about settings.xml. settings.xml is equivalent to a global configuration for maven and is used for all projects. There are two settings.xml in maven2, one located under the installation directory conf of maven2, as a global configuration. For team settings, maintaining a consistent definition is key, so the settings.xml under maven2/conf is used as a common configuration file for the team. Ensure that all team members have the same configuration. Of course, for each member, special custom settings, such as user information, so another settings.xml is used as a local configuration. The default location is: user.dir/.m2/settings.xml directory ({user.dir} refers to the user directory in windows).
The basic structure of settings.xml is as follows:
<settings 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/settings-1.0.0.xsd"> <localRepository/> <interactiveMode/> <usePluginRegistry/> <offline/> <pluginGroups/> <servers/> <mirrors/> <proxies/> <profiles/> <activeProfiles/</settings>
A brief introduction to several main configuration factors:
localRepository
It indicates the local library's storage location, that is, the main jar storage location of maven2. The default is ${user.dir}/.m2/repository. If you need to set it separately, it will be replaced with another path.
Offline offline
If you don't want to search for the remote center library every time you compile, set it to true. Of course, the premise is that you have downloaded the required dependency package.
Servers
The distributionManagement element in the POM defines the development library. However, specific username and pwd cannot be used in pom.xml, so server information is saved through this configuration
<servers> <server> <id>server001</id> <username>my_login</username> <password>my_password</password> <privateKey>${usr.home}/.ssh/id_dsa</privateKey> <passphrase>some_passphrase</passphrase> <filePermissions>664</filePermissions> <directoryPermissions>775</directoryPermissions> <configuration></configuration> </server> </servers>Mirrors
Represents a mirror library, a mirror of a specified library, used to add other libraries
<mirrors> <mirror> <id>planetmirror.com</id> <name>PlanetMirror Australia</name> <url>http://downloads.planetmirror.com/pub/maven2</url> <mirrorOf>central</mirrorOf> </mirror> </mirror>
Proxies
This setting is mainly used for library user configurations that cannot be directly accessed to the center.
<proxies> <proxy> <id>myproxy</id> <active>true</active> <protocol>http</protocol> <host>proxy.somewhere.com</host> <port>8080</port> <username>proxyuser</username> <password>somepassword</password> <nonProxyHosts>*.google.com|ibiblio.org</nonProxyHosts> </proxy> </proxies>
Profiles
Similar to the profile element in pom.xml, it mainly includes activation, repositories, pluginRepositories and properties elements
When you first get in touch, you may be confused. In fact, this is a relatively powerful feature in maven2. Literally speaking, it is a personal configuration.
After defining the profile separately, it will not take effect and needs to be activated by meeting the conditions.
repositories and pluginRepositories
Define other development libraries and plug-in development libraries. For the team, it must have its own development library. This configuration can be defined.
The following configuration defines the local development library for release.
<repositories> <repository> <id>repo-local</id> <name>Internal Development Library</name> <url>http://192.168.0.2:8082/repo-local</url> <releases> <enabled>true</enabled> <updatePolicy>never</updatePolicy> <checksumPolicy>warn</checksumPolicy> </releases> <snapshots> <enabled>false</enabled> </snapshots> <layout>default</layout> </repository> </repository> </repository> <pluginRepository> <id>repo-local</id> <name>Internal Development Library</name> <url>http://192.168.0.2:8082/repo-local</url> <releases> <enabled>true</enabled> <updatePolicy>never</updatePolicy> <checksumPolicy>warn</checksumPolicy> </releases> <snapshots> <enabled>false</enabled> </snapshots> <layout>default</layout> </pluginRepository> </pluginRepository> </pluginRepositories>
releases, snapshots: Release or snapshot of each product version (Note: the difference between release and snapshot, release is generally a relatively stable version, while snapshot is basically unstable, just as a snapshot)
Properties
maven's properties are used as placeholder values, such as ant's properties.
Includes the following 5 types of values:
1. env.X, return the current environment variable
2. project.x: Return the element value defined in the pom, such as project.version
3. settings.x: Return the element defined in settings.xml
4. java system properties: all values returned by java.lang.System.getProperties()
5. x: The value set by the user
Activation
Used to activate this profile
<activation> <activeByDefault>false</activeByDefault> <jdk>1.5</jdk> <os> <name>Windows XP</name> <family>Windows</family> <arch>x86</arch> <version>5.1.2600</version> </os> <property> <name>mavenVersion</name> <value>2.0.3</value> </property> <file> <exists>${basedir}/file2.properties</exists> <missing>${basedir}/file1.properties</missing> </file> </activation>In addition to using activation to activate profiles, you can also activate it through activeProfiles.
Active Profiles
Indicates the activated profile, specified by the profile id.
<activeProfiles> <activeProfile>env-test</activeProfile> Specified profile id </activeProfiles>
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.