环境
maven:3.3.9
java:1.8
操作系统:win7
maven的目录结构
src
-main
-java
-package
-test
-java
-package
resource
假设不使用任何工具的话,我们可以先手动创建目录,再到相关目录中写代码。
常用命令
mvn -v #查看版本信息
mvn compile #编译
mvn test # 测试
mvn package #打包成jar的形式
mvn clear #删除target
mvn install #安装jar包到本地仓库
创建项目目录的方式
如果每次我们都要手动去创建文件夹,这样会累死人,所以我们可以使用maven自带的插件,来帮我们快速的创建一个maven项目(的目录结构)。
第一次使用mvn archetype:generate
会去下载有很多依赖。
archetype:generate
执行以下命令可以快速创建一个maven项目
mvn archetype:generate
但是呢?不出意外,大家都会卡在:
[info]Generating project in interactive mode
这个地方。
要过很久很久才会显示出:
到了这里,要我们选择模板:
Choose a number: 6//我们输入6
之后我们还要填写,groupId、artifactId、version、package。
其中最后package
,是在src/main/java/包名
与src/test/java/包名
。
这里我们需要注意的是,要是我们不想去等卡住的那个地方:
[info]Generating project in interactive mode
我们就需要加参数。之所以会被卡住,是因为maven会去远程服务器上去找catalog
。我们就不让它去找。
mvn archetype:generate -DarchetypeCatalog=internal
这种方式执行出来的效果稍稍有些不一样:
这里它会默认选择7。接下来配置groupId
,artifactId
,version
,package
。是和上面一样的。
之后他又会要我们去确认,我们之前配置的信息。输入y
即可。
之后,一个maven的文件夹结构就生成完毕。
仓库
本地仓库
本地仓库:是我们在编译项目是,maven自动下载的jar包,存放在本机的地方。
远程仓库
远程仓库,可以自己配置别人存放jar包的路径。
当然apache有一个全球性的中央仓库。我们可以查看maven的默认配置来看,其路径。
我们在maven安装路径中的lib
文件夹中找到maven-model-builder-3.2.1.jar
maven-model-builder-3.2.1.jar\org\apache\maven\model
找到pom.xml
,打开,可以看到如下:
<repositories>
<repository>
<id>central</id>
<name>Central Repository</name>
<url>http://repo.maven.apache.org/maven2</url>
<layout>default</layout>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
①id,是该中央仓库的唯一标识。(central)
②name ,是该中央仓库的名字。就是人能看懂的名字。
③url,该中央仓库的地址。
镜像仓库
所谓镜像仓库:就是一个和中央仓库功能一样的仓库。通俗点来说,就是中央仓库有的,镜像仓库也有。
镜像仓库是为了提高下载速度而存在的,由于中央仓库是在国外的服务器上,在天朝连接外网,是件很蛋疼的事情,所以可以考虑使用国内的镜像仓库。‘
国内镜像仓库
阿里云:http://maven.aliyun.com/nexus/content/groups/public/
该如何配置呢?
maven安装目录 –> conf –> settings.xml文件。
找到mirrors
:
<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>maven.aliyun.com</id>
<!-- 配置仓库的唯一标识 -->
<mirrorOf>central</mirrorOf>
<!-- 要替换那个仓库,我这里替换默认中央仓库 -->
<name>central mirror in china</name>
<!-- 取一个人类可以看懂的名字 -->
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<!-- 国内镜像的路径 -->
</mirror>
</mirrors>
修改本地仓库的路径
假设重装系统的话,默认路径的本地仓库就会消失掉。
修改方法:
路径:
maven安装目录 –> conf –> settings.xml文件。
<!-- 这里是更改本地仓库的配置 -->
<localRepository>J:/javaproject/repo</localRepository>
<!--路径根据自己的情况来填写。-->
由于修改了本地仓库的路径,假设我们升级了maven,会导致我们之前的配置会被覆盖掉,所以建议把conf/settings.xml
文件拷贝到新的本地仓库地址中。
比如我的本地仓库地址为J:\javaproject\maven-repository
那么我就把settings.xml文件,拷贝到这个路径下。
eclipse中配置本地maven
在eclipse4.0+版本中都自带了maven。所以我们需要做的就是把eclipse中maven关联到我们本机maven。(之前,我们还要先去安装maven插件)
1、把本机maven安装路径配置到eclipse中。
2、再对User Settings进行配置,要是不进行配置,就是使用默认的!
由于我把本地仓库的路径改了,并且还配置了jdk的版本,所以我这里要进行配置。
在User Settings
页面中点击Global Settings
的Browse...
按钮。
选择本地仓库中的settings.xml
。
注意:本地仓库本来是没有settings.xml
配置文件的,是我从maven安装目录的conf/settings.xml
中拷贝过来的。
至此修改完毕。
问题
假设现在我们就利用eclipse来创建maven项目时,会出现两个问题:
问题一、
eclipse在其POM文件的一处提示出错如下:
Plugin execution not covered by lifecycle configuration: org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (execution: default-compile, phase: compile)
问题二、jdk默认为1.5
我们先来解决第一个问题:
1.进入Window—>Preferences—>Maven配置,进入Lifecycle Mapping设置项
从上图可以看出m2e管理maven生命周期的文件名是lifecycle-mapping-metadata.xml,以及该文件的存放路径
2.下一步我们就要去相应路径修改lifecycle-mapping-metadata.xml文件,但会发现这个文件在上图中提示的位置并不存在,那么此时就
以到eclipse的安装目录下的plugins下的org.eclipse.m2e.lifecyclemapping.defaults_xxxxxx.jar文件中找到该文件(如下图):
通过解压软件可以发现lifecycle-mapping-metadata.xml文件的确在jar包中,把它从jar包中解压出来并放置到前图所示的路径下
3.打开lifecycle-mapping-metadata.xml文件,把未识别的插件在文件中加入即可:
<pluginExecution>
<pluginExecutionFilter>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<goals>
<goal>resources</goal>
<goal>testResources</goal>
<goal>copy-resources</goal>
</goals>
<versionRange>[2.4,)</versionRange>
<! --这里是我添加的 start -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<goals>
<goal>testCompile</goal>
<goal>compile</goal>
</goals>
<versionRange>[3.2,)</versionRange>
<! --这里是我添加的 end -->
</pluginExecutionFilter>
<action>
<execute>
<runOnIncremental>true</runOnIncremental>
</execute>
</action>
</pluginExecution>
我在里面添加的:
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<goals>
<goal>testCompile</goal>
<goal>compile</goal>
</goals>
<versionRange>[3.2,)</versionRange>
4.修改完成后,需在maven
配置处把“Update Maven projects on startup”
选项勾上,并重启eclipse即可消除出错示。重启后,再对项目右键 — maven — update project ...
。
在POM配置Maven plugin提示错误“Plugin execution not covered by lifecycle configuration”的解决方案
修改jdk版本
由于eclipse中创建maven默认版本是jdk1.5,现在都1.8了,我们肯定是要进行修改的。
由于我们在maven – User Settings 中设置的是本地仓库中的settings.xml
.
所以我们需要对该文件进行修改。
我们先找到profiles
标签。
我们再添加:
<profile>
<id>jdk-1.8</id>
<activation>
<activeByDefault>true</activeByDefault>
<jdk>1.8</jdk>
</activation>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
</properties>
</profile>
其中标签properties
在注释中是没有的。但是最好也把它配置上。
最后我完整的配置如下:
<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.8</id>
<activation>
<activeByDefault>true</activeByDefault>
<jdk>1.8</jdk>
</activation>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
</properties>
</profile>
</profiles>
配置好了后,记得最好也把maven安装目录中的conf/settings.xml
也做相应的修改。
最后一定要重启eclipse,再对项目 右键 — maven
— update project
即可看到变化。