Eclipse下Maven新建專案、自動打依賴jar包(包含普通專案和Web專案)
當我們無法從本地倉庫找到需要的構件的時候,就會從遠端倉庫下載構件至本地倉庫。一般地,對於每個人來說,書房只有一個,但外面的書店有很多,類似第,對於Maven來說,每個使用者只有一個本地倉庫,但可以配置訪問很多遠端倉庫。
強烈推薦書籍:Maven實戰 許曉斌著。目前是第一版
注意:最新版本的Eclipse代號Mars,已經直接集成了Maven,所以無需安裝m2Eclipse外掛。
Eclipse下新建Maven專案
1、下載Maven安裝包
選擇當前最新版本:"apache-maven-3.3.9-bin.zip",下載到本地,解壓縮到本地磁碟下。
2、配置
2.1 環境變數的配置
2.2 修改預設的本地倉庫位置
進入Maven安裝目錄下的conf子目錄中,開啟settings.xml進行配置修改。
<!-- localRepository
| The path to the local repository maven will use to store artifacts.
|
| Default: ${user.home}/.m2/repository
<localRepository>/path/to/local/repo</localRepository>
-->
<localRepository>D:/SoftWare/maven/repository</localRepository>
Maven預設的本地倉庫位置是當前使用者工作目錄下的".m2/repository",使用過程中這個目錄裡的檔案會比較多,佔用空間越來越大。一般建議更換到其它磁碟目錄下。如下配置,就把預設的本地倉庫更改到自己建立的目錄。
如下圖所示的localRepository標籤中設定,就把預設的本地倉庫更改到D:/SoftWare/maven/repository這個目錄(這個目錄結構需要自己建立好)。
2.3修改預設的中央倉庫映象
Maven預設的中央倉庫裡的檔案不全。所以,都需要自行新增其它的映象地址。在settings.xml檔案中的“<mirrors>”標籤裡新增如下內容:
注意啦!有時候,上面這樣配置,不穩定,我這裡給大家分享一個穩定的setting.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: ${user.home}/.m2/repository
<localRepository>/path/to/local/repo</localRepository>
-->
<localRepository>D:/SoftWare/maven/repository</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>
-->
<mirror>
<id>osc</id>
<mirrorOf>central</mirrorOf>
<url>http://maven.aliyun.com/nexus/content/repositories/central</url>
</mirror>
<mirror>
<id>osc_thirdparty</id>
<mirrorOf>thirdparty</mirrorOf>
<url>http://maven.aliyun.com/nexus/content/repositories/central</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>
-->
<profile>
<id>jdk-1.4</id>
<activation>
<jdk>1.4</jdk>
</activation>
<repositories>
<repository>
<id>nexus</id>
<name>local private nexus</name>
<url>http://maven.aliyun.com/nexus/content/repositories/central</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>nexus</id>
<name>local private nexus</name>
<url>http://maven.aliyun.com/nexus/content/repositories/central</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
<profile>
<id>osc</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<repositories>
<repository>
<id>osc</id>
<url>http://maven.aliyun.com/nexus/content/repositories/central</url>
</repository>
<repository>
<id>osc_thirdparty</id>
<url>http://maven.aliyun.com/nexus/content/repositories/central</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>osc</id>
<url>http://maven.aliyun.com/nexus/content/repositories/central</url>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
<!-- activeProfiles
| List of profiles that are active for all builds.
|
<activeProfiles>
<activeProfile>alwaysActiveProfile</activeProfile>
<activeProfile>anotherAlwaysActiveProfile</activeProfile>
</activeProfiles>
-->
</settings>
儲存所做的修改,同時還需要把這個settings.xml檔案複製一份到“D:\SoftWare\maven”目錄下。
3、eclipse裡的Maven外掛安裝
Eclipse預設不支援Maven。需要給它新增m2eclipse外掛。下面是具體的操作步驟。
3.1、外掛安裝
開啟Eclipse的Help->Install New Software,如下圖所示:
選擇“Add..”按鈕,又會彈出如下對話方塊:
這個對話方塊就是用於新增一個外掛地址的。在“Name”對應的輸入框裡輸入該操作的一個標識名。在“Location”對應的輸入框裡輸入這個外掛的安裝地址。注:Maven的Eclipse外掛地址為:http://download.eclipse.org/technology/m2e/releases。輸入後,如下圖所示:
Name: m2e
Location: http://download.eclipse.org/technology/m2e/releases
輸入完成後,點選右下角的“OK”按鈕,就會彈出如下對話方塊:
MARS版本
我這裡,就,已經安裝過了。
這一步,有可能會
注意:安裝maven時一定要注意版本匹配問題:下面就是我安裝時遇到的問題
首先安裝maven時遇到Missing requirement: m2e logback configuration 1.7.0.20160603-1933 (org.eclipse.m2e.logback.configur。。。的問題。
推薦用MARS版本!!!
注意,有些版本,我已測試過,如,NEON版本
會發生上述的錯誤。
這裡需要選擇想要安裝的外掛的詳細內容。選中“Maven Integration for Eclipse”前面的複選框。如下圖所示:
選擇完成後,點選右下方的“Next>”按鈕進行安裝。安裝成功後,會提示需要重啟Eclipse來生效這次配置。重啟之後,這個Eclipse就可以使用m2eclipse外掛功能了。
需要一段時間
自動重啟
3.2、Eclipse下對maven進行配置
外掛安裝好之後,還需要進行一些配置才能夠開始使用Maven的功能。
3.2.1、 設定m2eclipse外掛所關聯的Maven程式
需要修改配置:首先選擇Window->Preferences,彈出如下對話方塊
在這個對話方塊左邊的導航欄中展開“Maven”目錄結點,並選擇“Installations”子節點,如下圖所示:
這裡需要關聯上對應的Maven安裝程式。具體操作是,在右邊面板中選擇“Add…”按鈕,會彈出如下選擇Maven安裝目錄的選擇對話方塊:
通過這個對話方塊來選擇Maven的具體安裝目錄。選擇好之後就點選“確定”按鈕。如下圖所示:
3.2.2、 設定自定義的本地倉庫
選擇如上圖對話視窗左邊選單樹 “Maven”節點下的“UserSettings”子節點,它預設的配置檔案是“C:\Documents and Settings\csdn\.m2\settings.xml”,如下圖所示:
這邊需要修改為我們自定義倉庫位置下的settings.xml檔案。具體操作是,點選“Browse…”按鈕來選擇上一步驟修改的本地倉庫目錄下的settings.xml檔案,如下圖所示:
在上圖中選擇D:\SoftWare\apache-maven-3.3.9\conf目錄下的“settings.xml”檔案,點選“開啟”按鈕來確認剛才的選擇,就會回到如下圖的對話視窗中:
在上圖中點選右下角的“OK”按鈕來完成所有的配置修改。
4、Eclipse建立maven專案
在eclipse選單欄中選擇"File"->"News"->"Other"選單項,就會開啟如下對話視窗,在視窗中選擇"Maven"節點下"Maven Project"條目,如下圖所示,之後跟著下一步。。。就可以將專案建立好了。
我這裡,選擇other
想說的是,SBT是為scala專門而設計的,但是,一般很多人還是用的是maven。
使用maven-archetype-quickstart 或者 coccoon-22-archetype-webapp
或者
也許,這一步,會碰到如下問題。
解決辦法:
或者
Catalog File: http://repo2.maven.org/maven2/archetype-catalog.xml
Description: maven catalog
然後,再重新 File -> New -> Maven Project
這裡,自己任意取,
Group Id: com.zhouls.spark
Artifact Id: SparkApps
CoreException: Could not calculate build plan: Plugin org.apache.maven.plugins:maven-compiler-plugin:3.1 or one of its dependencies could not be resolved: Failed to read artifact descriptor for org.apache.maven.plugins:maven-compiler-plugin:jar:3.1: UnresolvableModelException: Failure to transfer org.apache.maven.plugins:maven-plugins:pom:24 from https://repo.maven.apache.org/maven2 was cached in the local repository, resolution will not be reattempted until the update interval of central has elapsed or updates are forced. Original error: Could not transfer artifact org.apache.maven.plugins:maven-plugins:pom:24 from/to central (https://repo.maven.apache.org/maven2): timeout
解決方法:
1.找到maven庫目錄,進入:D:\SoftWare\maven\repository\org\apache\maven\plugins\maven-compiler-plugin\3.1
2. 若3.1這個目錄下只有,"maven-compiler-plugin-3.1.pom.lastUpdated" 則需要到http://repo1.maven.org/maven2/org/apache/maven/plugins/maven-compiler-plugin/3.1/,把裡面的檔案下載下來放到目錄3.1下,如果該倉庫中沒有,那麼可以去http://search.maven.org/這個地方下載下來,然後放置到對應的本地倉庫中
可見,我這裡是不需進行步驟2,直接到步驟3.
3.刪除3.1下的maven-compiler-plugin-3.1.pom.lastUpdated檔案或3.1目錄下的所有檔案
4.專案右鍵-->maven-->Update Dependencies
即,得到解決!
如果以上方法還是無法解決該問題:則 開啟settings.xml,嘗試換個mirror映象或者乾脆採用預設的倉庫,即註釋掉
刪除清空
再重新來一次
這裡,我繼續,上面的步驟,下來。用了我那個穩定的setting.xml檔案,之後,中間不會出現什麼問題!!!
把此處預設的jdk,J2SE-1.5,變成我們自己安裝的jdk版本(jdk1.7或jdk1.8)
得到
對應,得到
這個pom.xml檔案,裡有我們工程實際時編寫和執行時的依賴和支援。
進入,如下
選擇,pom.xml
在修改pom.xml檔案之前,Maven Dependencies如下
建立專案之後,pom.xml預設的內容如下
<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.zhouls.spark</groupId>
<artifactId>SparkApps</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>SparkApps</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
這一步,最為關鍵,具體怎麼修改pom.xml,是要看自己的需求。
我這裡,因為是spark1.5.2版本。
其他的,類似,這裡不多贅述。
在這過程中,最好是去複製,官網。為了避免自己手誤或者格式不對!!!可以避免如下面的錯誤!
修改後的pom.xml內容是
<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.zhouls.spark</groupId>
<artifactId>SparkApps</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>SparkApps</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<junit.version>4.10</junit.version>
<spark.version>1.5.2</spark.version>
<hadoop.version>2.6.0</hadoop.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-core_2.10</artifactId>
<version>${spark.version}</version>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-sql_2.10</artifactId>
<version>${spark.version}</version>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-hive_2.10</artifactId>
<version>${spark.version}</version>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-streaming_2.10</artifactId>
<version>${spark.version}</version>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-streaming-kafka_2.10</artifactId>
<version>${spark.version}</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>${hadoop.version}</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>src/main/java</sourceDirectory>
<testSourceDirectory>src/main/test</testSourceDirectory>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass></mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<executable>java</executable>
<includeProjectDependencies>true</includeProjectDependencies>
<includePluginDependencies>false</includePluginDependencies>
<classpathScope>compile</classpathScope>
<mainClass>com.zhouls.spark.SparkApps.cores.WordCount</mainClass>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
正在下載
立馬生成好了。可見,Maven自動生成jar包,多麼的方便!
其他的一一去解決!
對應地,生成,
我的這裡,得到如下
至此,Eclipse下新建Maven專案、自動打依賴jar包。大功告成!!!
Eclipse裡建立Maven,比較有價值的錯誤總結
感謝如下博主:
關於Maven的進一步,深入學習,推薦