idea如何建立maven專案(二)

同類文章:idea如何建立maven專案(一)

這裡主要講講maven的settings.xml的一些常用配置,還有一些常用的生命週期。先說明一下,這篇部落格只講常用的,不做深入講解哈,說實話,很多東西,我工作或者自己的專案中也沒用到過。

1.settings.xml的常用配置

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

見圖(1)
這裡寫圖片描述

圖(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):

這裡寫圖片描述

圖(2)

常用的的就上面幾個配置吧,如果大家還想了解其他的可以看看這篇文章:
http://www.cnblogs.com/yangxia-test/p/4409736.html

2.maven常用生命週期

1)開啟Maven Projects

見圖(3):
這裡寫圖片描述

圖(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的高階應用肯定不止於此,大家如果想更上一層樓,還需要多多學習,本人目前能力也有限,就只能先寫到這裡了,望大家共勉。