这里主要讲讲maven的settings.xml的一些常用配置,还有一些常用的生命周期。先说明一下,这篇博客只讲常用的,不做深入讲解哈,说实话,很多东西,我工作或者自己的项目中也没用到过。

1.settings.xml的常用配置

1)首先找到自己使用的settings.xml文件的位置

见图(1)

idea 去掉maven地址 idea移除maven项目_xml


图(1)

2)打开settings.xml

代码如下:

<?xml version="1.0" encoding="UTF-8"?>

<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied.  See the License for the
specific language governing permissions and limitations
under the License.
-->

<!--
 | This is the configuration file for Maven. It can be specified at two levels:
 |
 |  1. User Level. This settings.xml file provides configuration for a single user,
 |                 and is normally provided in ${user.home}/.m2/settings.xml.
 |
 |                 NOTE: This location can be overridden with the CLI option:
 |
 |                 -s /path/to/user/settings.xml
 |
 |  2. Global Level. This settings.xml file provides configuration for all Maven
 |                 users on a machine (assuming they're all using the same Maven
 |                 installation). It's normally provided in
 |                 ${maven.home}/conf/settings.xml.
 |
 |                 NOTE: This location can be overridden with the CLI option:
 |
 |                 -gs /path/to/global/settings.xml
 |
 | The sections in this sample file are intended to give you a running start at
 | getting the most out of your Maven installation. Where appropriate, the default
 | values (values used when the setting is not specified) are provided.
 |
 |-->
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
  <!-- localRepository
   | The path to the local repository maven will use to store artifacts.
   |
   | Default: ~/.m2/repository
  <localRepository>/path/to/local/repo</localRepository>
  -->

  <!-- interactiveMode
   | This will determine whether maven prompts you when it needs input. If set to false,
   | maven will use a sensible default value, perhaps based on some other setting, for
   | the parameter in question.
   |
   | Default: true
  <interactiveMode>true</interactiveMode>
  -->

  <!-- offline
   | Determines whether maven should attempt to connect to the network when executing a build.
   | This will have an effect on artifact downloads, artifact deployment, and others.
   |
   | Default: false
  <offline>false</offline>
  -->

  <!-- pluginGroups
   | This is a list of additional group identifiers that will be searched when resolving plugins by their prefix, i.e.
   | when invoking a command line like "mvn prefix:goal". Maven will automatically add the group identifiers
   | "org.apache.maven.plugins" and "org.codehaus.mojo" if these are not already contained in the list.
   |-->
  <pluginGroups>
    <!-- pluginGroup
     | Specifies a further group identifier to use for plugin lookup.
    <pluginGroup>com.your.plugins</pluginGroup>
    -->
  </pluginGroups>

  <!-- proxies
   | This is a list of proxies which can be used on this machine to connect to the network.
   | Unless otherwise specified (by system property or command-line switch), the first proxy
   | specification in this list marked as active will be used.
   |-->
  <proxies>
    <!-- proxy
     | Specification for one proxy, to be used in connecting to the network.
     |
    <proxy>
      <id>optional</id>
      <active>true</active>
      <protocol>http</protocol>
      <username>proxyuser</username>
      <password>proxypass</password>
      <host>proxy.host.net</host>
      <port>80</port>
      <nonProxyHosts>local.net|some.host.com</nonProxyHosts>
    </proxy>
    -->
  </proxies>

  <!-- servers
   | This is a list of authentication profiles, keyed by the server-id used within the system.
   | Authentication profiles can be used whenever maven must make a connection to a remote server.
   |-->
  <servers>
    <!-- server
     | Specifies the authentication information to use when connecting to a particular server, identified by
     | a unique name within the system (referred to by the 'id' attribute below).
     |
     | NOTE: You should either specify username/password OR privateKey/passphrase, since these pairings are
     |       used together.
     |
    <server>
      <id>deploymentRepo</id>
      <username>repouser</username>
      <password>repopwd</password>
    </server>
    -->

    <!-- Another sample, using keys to authenticate.
    <server>
      <id>siteServer</id>
      <privateKey>/path/to/private/key</privateKey>
      <passphrase>optional; leave empty if not used.</passphrase>
    </server>
    -->
  </servers>

  <!-- mirrors
   | This is a list of mirrors to be used in downloading artifacts from remote repositories.
   |
   | It works like this: a POM may declare a repository to use in resolving certain artifacts.
   | However, this repository may have problems with heavy traffic at times, so people have mirrored
   | it to several places.
   |
   | That repository definition will have a unique id, so we can create a mirror reference for that
   | repository, to be used as an alternate download site. The mirror site will be the preferred
   | server for that repository.
   |-->
  <mirrors>
    <!-- mirror
     | Specifies a repository mirror site to use instead of a given repository. The repository that
     | this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used
     | for inheritance and direct lookup purposes, and must be unique across the set of mirrors.
     |
    <mirror>
      <id>mirrorId</id>
      <mirrorOf>repositoryId</mirrorOf>
      <name>Human Readable Name for this Mirror.</name>
      <url>http://my.repository.com/repo/path</url>
    </mirror>
     -->

  </mirrors>

  <!-- profiles
   | This is a list of profiles which can be activated in a variety of ways, and which can modify
   | the build process. Profiles provided in the settings.xml are intended to provide local machine-
   | specific paths and repository locations which allow the build to work in the local environment.
   |
   | For example, if you have an integration testing plugin - like cactus - that needs to know where
   | your Tomcat instance is installed, you can provide a variable here such that the variable is
   | dereferenced during the build process to configure the cactus plugin.
   |
   | As noted above, profiles can be activated in a variety of ways. One way - the activeProfiles
   | section of this document (settings.xml) - will be discussed later. Another way essentially
   | relies on the detection of a system property, either matching a particular value for the property,
   | or merely testing its existence. Profiles can also be activated by JDK version prefix, where a
   | value of '1.4' might activate a profile when the build is executed on a JDK version of '1.4.2_07'.
   | Finally, the list of active profiles can be specified directly from the command line.
   |
   | NOTE: For profiles defined in the settings.xml, you are restricted to specifying only artifact
   |       repositories, plugin repositories, and free-form properties to be used as configuration
   |       variables for plugins in the POM.
   |
   |-->
  <profiles>
    <!-- profile
     | Specifies a set of introductions to the build process, to be activated using one or more of the
     | mechanisms described above. For inheritance purposes, and to activate profiles via <activatedProfiles/>
     | or the command line, profiles have to have an ID that is unique.
     |
     | An encouraged best practice for profile identification is to use a consistent naming convention
     | for profiles, such as 'env-dev', 'env-test', 'env-production', 'user-jdcasey', 'user-brett', etc.
     | This will make it more intuitive to understand what the set of introduced profiles is attempting
     | to accomplish, particularly when you only have a list of profile id's for debug.
     |
     | This profile example uses the JDK version to trigger activation, and provides a JDK-specific repo.
    <profile>
      <id>jdk-1.4</id>

      <activation>
        <jdk>1.4</jdk>
      </activation>

      <repositories>
        <repository>
          <id>jdk14</id>
          <name>Repository for JDK 1.4 builds</name>
          <url>http://www.myhost.com/maven/jdk14</url>
          <layout>default</layout>
          <snapshotPolicy>always</snapshotPolicy>
        </repository>
      </repositories>
    </profile>
    -->

    <!--
     | Here is another profile, activated by the system property 'target-env' with a value of 'dev',
     | which provides a specific path to the Tomcat instance. To use this, your plugin configuration
     | might hypothetically look like:
     |
     | ...
     | <plugin>
     |   <groupId>org.myco.myplugins</groupId>
     |   <artifactId>myplugin</artifactId>
     |
     |   <configuration>
     |     <tomcatLocation>${tomcatPath}</tomcatLocation>
     |   </configuration>
     | </plugin>
     | ...
     |
     | NOTE: If you just wanted to inject this configuration whenever someone set 'target-env' to
     |       anything, you could just leave off the <value/> inside the activation-property.
     |
    <profile>
      <id>env-dev</id>

      <activation>
        <property>
          <name>target-env</name>
          <value>dev</value>
        </property>
      </activation>

      <properties>
        <tomcatPath>/path/to/tomcat/instance</tomcatPath>
      </properties>
    </profile>
    -->
  </profiles>

  <!-- activeProfiles
   | List of profiles that are active for all builds.
   |
  <activeProfiles>
    <activeProfile>alwaysActiveProfile</activeProfile>
    <activeProfile>anotherAlwaysActiveProfile</activeProfile>
  </activeProfiles>
  -->
</settings>

此时的settings.xml是原生的,什么都没有配置,如果读者有谁找不到settings.xml文件,可以自己创建一个settings.xml粘贴以上代码进去就行了。

3)设置maven镜像地址

这是我认为非常重要的一步,因为如果不指定镜像地址(或者自己的maven私有库),maven或默认从外国的服务器上拉取jar包,这是非常慢的(不信你们在之后下载maven工具时不使用镜像地址试试)。
设置方式非常简单,在</mirrors> </mirrors>中间加入以下代码:

<mirror>  
      <id>alimaven</id>  
      <name>aliyun maven</name>  
      <url>http://maven.aliyun.com/nexus/content/groups/public/</url>  
      <mirrorOf>central</mirrorOf>          
    </mirror>

再保存,就行了。我这里使用的是淘宝镜像,本人亲测速度非常不错,其他镜像的代码你们可以上百度搜索,很容易就能搜索到的。

4)maven私有库设置

maven私有仓库,顾名思义,就是自己内部人员使用的,一般大型的公司会在自己的服务器上架设一个maven仓库,内部员工的jar包可以上传到上面,也可以从上面下载jar包,这样jar包的版面易于管理,而且下载上传速度非常快。这个的配置(这里只说使用,不谈如何架设maven仓库哈,如果有需要,以后会写git仓库和maven仓库的搭建教程)也很简单,就以我们公司的为例吧(当然补分代码用xxx代替了)。
只需要设置<servers></servers><profiles></profiles>就行了,参考代码如下:

<servers>
    <server>
        <id>snapshots</id>
        <username>xxxx</username>
        <password>xxxx</password>
    </server>
    <server>
        <id>tomcat7x</id>
        <username>xx</username>
        <password>xxx</password>
    </server>
</servers>

<profiles>
<profile>
    <id>wsbp</id>
    <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>
            <id>wsbp</id>
            <name>local private wsbp</name>
            <url>http: //192.xxx.xxx.xxx:xxxx/nexus/xxx/groups/xxx</url>
            <releases>
                <enabled>true</enabled>
                <updatePolicy>always</updatePolicy>
                <checksumPolicy>warn</checksumPolicy>
            </releases>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>wsbp</id>
            <name>local private wsbp</name>
            <url>http: //192.168.xxx.xxx:xxxx/nexus/content/xxxx/xxxx</url>
            <releases>
                <enabled>true</enabled>
                <updatePolicy>always</updatePolicy>
                <checksumPolicy>warn</checksumPolicy>
            </releases>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>
</profile>
<profile>
    <id>public</id>
    <repositories>
        <repository>
            <id>public</id>
            <name>local private public</name>
            <url>http: //192.168.xxx.xxx:xxxx/nexus/content/xxxxxxxx</url>
            <releases>
                <enabled>true</enabled>
                <updatePolicy>always</updatePolicy>
                <checksumPolicy>warn</checksumPolicy>
            </releases>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>public</id>
            <name>local private public</name>
            <url>http: //192.168.xxx.xxx:xxxx/nexus/content/xxxxx/xxxx</url>
            <releases>
                <enabled>true</enabled>
                <updatePolicy>always</updatePolicy>
                <checksumPolicy>warn</checksumPolicy>
            </releases>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>
</profile>
</profiles>

可能看起来有点多,但分析标签名就知道,无非是一些仓库地址和用户名、密码而已,而且这些代码,一般公司会提供给你,不用自己写,所以你只管粘贴上去,大概知道是干什么的就行了。

5)配置本地仓库地址

就是设置你拉取到的jar包的位置,每次maven拉取jar包的时候会扫描本地仓库,如果有,就直接使用,如果没有才会从网上拉取,代码片段如下:

<localRepository>D:\Java\m2 \repository</localRepository>

此代码表示仓库目录为D:\Java\m2 \repository。

idea也可以直接在工具中设置(eclipse不行,eclipse的仓库目录直接以settings.xml中设置的为准,如果没设置windows的话好像是在C:\Users\Administrator.m2\repository这里,很久没用eclipse了,记不太得了,有兴趣你们可以去看看),具体操作见图(2):

idea 去掉maven地址 idea移除maven项目_idea_02


图(2)

常用的的就上面几个配置吧,如果大家还想了解其他的可以看看这篇文章:

2.maven常用生命周期

1)打开Maven Projects

见图(3):

idea 去掉maven地址 idea移除maven项目_idea 去掉maven地址_03


图(3)


每一个生命周期都可以单独双击执行。

2)clean

这里的clean和eclipse中tomcat的clean差不多,一般我习惯启动项目前就clean一下,主要是为了移除所有上一次构建生成的文件,小心一点总是好的。

3)compile

这个就顾名思义了吧,编译项目,一般启动项目之前编译一下,不报错再启动,报错就解决问题。不过如果你不编译,直接启动项目,工具也会先编译项目,不过这样的话,怎么说呢,就好像不够专业的感觉,看个人习惯吧,其实说实话没什么差别。

4)test

用于单元测验,我没怎么用过,这里就不乱说了,免得误人子弟。

5)package

顾名思义,打包,这个用得较多,一般分为war包和jar包,如果是web项目,一般打包为war包,普通项目,就打包为jar包,如果普通项目打包为war包会报错,因为找不到web.xml文件。
那么如何设置打包类型呢,在pom.xml中,代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<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.ooo</groupId>
    <artifactId>test</artifactId>
    <version>1.0-SNAPSHOT</version>

    <packaging>jar</packaging>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.0-b01</version>
        </dependency>
    </dependencies>

</project>

可以看见有一个<packaging>jar</packaging>,如果想打包为war包,只需要改为<packaging>war</packaging>就行了。

我经常用到的就这几个,其他的确实没怎么用过,如果大家想更深入地学习,可以去Google一下maven的生命周期详解,另外提醒一下,如果想要deploy,必须要在settings.xml中配置<distributionManagement></distributionManagement>,不然maven不知道 你要deploy到什么地方去。

6)关于plugin

在使用idea的Maven Projects的生命周期的时候,工具会自动下载相关的maven plugin(第一次使用的时候,你把settings.xml中配置的镜像取消试试,起码下载几个小时),但是eclipse的话不会,需要在pom.xml中配置相应地plugin,示例代码如下:

<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-deploy-plugin</artifactId>
                <version>2.7</version>
                <configuration>
                    <uniqueVersion>false</uniqueVersion>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-eclipse-plugin</artifactId>
                <version>2.8</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <failOnError>true</failOnError>
                    <verbose>true</verbose>
                    <fork>true</fork>
                    <compilerArgument>-nowarn</compilerArgument>
                    <source>1.6</source>
                    <target>1.6</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <version>2.1.2</version>
                <executions>
                    <execution>
                        <id>attach-sources</id>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

<build></build>就在<project></project>的下一级,这里只举例了几个常用的插件,如果需要其他的可以网上搜索对应代码,加到<plugins></plugins>中就行了。

3.结束

关于maven,就先介绍到这里吧,我感觉知道这些之后,项目在maven上的问题应该就不多了,当然,maven的高级应用肯定不止于此,大家如果想更上一层楼,还需要多多学习,本人目前能力也有限,就只能先写到这里了,望大家共勉。