eclipse MAVEN插件正确使用方式。

来源:互联网 发布:数据透视表有什么用处 编辑:程序博客网 时间:2024/04/30 09:26
  • 首先贴出我的MAVEN的setting.xml文件,嗯,我也怕忘了下次再怎么去配置。
<?xml version="1.0" encoding="UTF-8"?><!--Licensed to the Apache Software Foundation (ASF) under oneor more contributor license agreements.  See the NOTICE filedistributed with this work for additional informationregarding copyright ownership.  The ASF licenses this fileto you under the Apache License, Version 2.0 (the"License"); you may not use this file except in compliancewith the License.  You may obtain a copy of the License at    http://www.apache.org/licenses/LICENSE-2.0Unless 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 ANYKIND, either express or implied.  See the License for thespecific language governing permissions and limitationsunder 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>E:/Repositories</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>    -->    <server>        <id>releases</id>        <username>admin</username>        <password>admin123</password>    </server>    <server>        <id>snapshots</id>        <username>admin</username>        <password>admin123</password>    </server>    <!-- 自动打包部署到tomcat,用户名和密码对应tomcat/conf/tomcat-users.xml的配置 -->    <server>        <id>tomcat7</id>        <username>manager</username>        <password>cc830905</password>    </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.-->     <!-- 配置maven创建工程时使用的JDK编译版本 -->    <profile>      <id>jdk17</id>       <activation>            <activeByDefault>true</activeByDefault>            <jdk>1.7</jdk>       </activation>       <properties>            <maven.compiler.source>1.7</maven.compiler.source>            <maven.compiler.target>1.7</maven.compiler.target>            <maven.compiler.compilerVersion>1.7</maven.compiler.compilerVersion>       </properties>     </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>dev</id>      <repositories>        <repository>          <id>local-nexus</id>          <url>http://cc.thiscc.com:8081/nexus/content/groups/public/</url>          <releases>            <enabled>true</enabled>          </releases>          <snapshots>            <enabled>true</enabled>          </snapshots>        </repository>      <repository>        <id>people.apache.snapshots</id>        <url>        http://repository.apache.org/content/groups/snapshots-group/        </url>    <releases>        <enabled>false</enabled>    </releases>    <snapshots>        <enabled>true</enabled>    </snapshots></repository>      </repositories>    </profile>    </profiles><activeProfiles>    <activeProfile>dev</activeProfile>  </activeProfiles>  <!-- activeProfiles   | List of profiles that are active for all builds.   |  <activeProfiles>    <activeProfile>alwaysActiveProfile</activeProfile>    <activeProfile>anotherAlwaysActiveProfile</activeProfile>  </activeProfiles>  --></settings>

使用eclipse创建maven web工程的步骤:

  1. new–>Project…–>Maven Project
    这里写图片描述
    这里写图片描述
    这里写图片描述
  2. 配置项目运行容器tomcat
    这里写图片描述
    这里写图片描述
    这里写图片描述

  • 问题: 在Eclipse中新建了一个Maven工程, 默认生成的JDK版本是1.5,然后更改JDK版本为1.7, 结果每次使用Maven > Update project的时候JDK版本都恢复成1.5。
    • 原因:
      • 这是Maven已知的一个特性。除非在你的POM文件中显示的指定一个版本,否则会使用编译器默认的source/target版本1.5。主要还是在于Eclipse中Maven的集成方式起到了关键作用, 它会从POM文件中生成项目的.project,.classpath以及.settings, 因此除非POM文件指定了正确的JDK版本, 否则你每次更新项目配置的时候它都会重置到1.5版本。
    • 解决办法: 在项目的pom.xml文件中增加如下配置:
<build>    <plugins>      <plugin>        <groupId>org.apache.maven.plugins</groupId>        <artifactId>maven-compiler-plugin</artifactId>        <version>3.1</version>        <configuration>          <source>1.7</source> <!-- 源代码使用的开发版本 -->          <target>1.7</target> <!-- 需要生成的目标class文件的编译版本 -->          <!-- 一般而言,target与source是保持一致的,但是,有时候为了让程序能在其他版本的jdk中运行(对于低版本目标jdk,源代码中需要没有使用低版本jdk中不支持的语法),会存在target不同于source的情况 -->          <encoding>UTF8</encoding>           <!--windows默认使用GBK编码,java项目经常编码为utf8,也需要在compiler插件中指出,否则中文乱码可能会出现编译错误-->        </configuration>      </plugin>    </plugins></build>

  • 技巧:上面的方法每次都要去生成的工程中去修改pom,项目多的话修改起来很麻烦,这里有个一劳永逸的办法,修改eclipse 创建maven项目设置默认jdk版本

    • 只需要在maven的setting.xml文件中 的 profiles 节点 加入如下代码片段:
<profile>      <id>jdk17</id>       <activation>            <activeByDefault>true</activeByDefault>            <jdk>1.7</jdk>       </activation>       <properties>            <maven.compiler.source>1.7</maven.compiler.source>            <maven.compiler.target>1.7</maven.compiler.target>            <maven.compiler.compilerVersion>1.7</maven.compiler.compilerVersion>       </properties>   </profile>  

  • 技巧:创建支持Java Servlet3.0的Maven Webapp项目
    • 首先明确一个概念:dynamic web module version与tomcat以及JDK之间的对应关系
dynamic web module version 对应的tomcat 对应的J2EE 2.3 未知 JAVA1.3以上 2.4 tomcat5.5以上 JAVA1.3以上 2.5 tomcat6以上 JAVA1.5以上 3.0 tomcat7以上 JAVA1.6以上 3.1 tomcat8以上 JAVA1.7以上

这里我也不是很懂,找了半天资料都没搞明白这什么玩意o(╯□╰)o,我的理解大概就是如果你用的dynamic web module是3.0的,那么你把这个项目部署在tomcat6里面去运行是不行滴,用1.7版本一下的JDK也是不行滴,嗯,大概就是这样。理解错了欢迎给我指正。

这个技巧是由一个问题引出来的:那就是当你右键eclipse的工程,选择属性,再选择Project Facets里面中选择Dynamic Web Module,想修改这个Module版本的时候,提示你:

Cannot change version of project facet Dynamic Web Module to 3.0

就是死活不让修改!那我们只能暴力执法了→_→,去修改配置文件:
这里写图片描述

另外这里还有另一种解决方式,我就不赘述了,英语比较好的去参考一下歪果仁的思路—>传送门

到这里还不算完,还有一个地方需要我们去修改一下,那就是web.xml文件。
这里写图片描述
当你修改Dynamic Web Module后,Update Project…后就会看到上面这样子的图。这是web.xml声明中的版本和Dynamic Web Module不一致造成的,那么我们就来修改一下web.xml这个声明:
这里写图片描述

方便伸手党copy的代码在下面:

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xmlns="http://java.sun.com/xml/ns/javaee"    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"    id="WebApp_ID" version="3.0">    <display-name>xcrm</display-name></web-app>

再次右键工程–>Maven–>Update Project…,大功告成,一个没有红叉叉的maven web项目就这样完成了。


哎,另一种方式也记录一下吧,就是上面传送门歪果仁用的方法,照顾一下英语和我一样渣的童鞋。。最主要的区别就是,这种方式可以直接在eclipse里面完成,而不用重启eclipse。进入正题:

  1. 创建maven project,没什么好说的,大把的教程了。
  2. 右键工程–>properties–>project facets,此时如下:
    这里写图片描述
    我们要做的就是先把Dynamic Web Module的勾去掉,再Apply。

  3. 此时我们再去把它勾上,现在你就可以任意选择版本了,如下:
    这里写图片描述
    点击Apply之后,此时我们会发现,在工程目录里面多了个WebContent目录。

  4. 把它自动给我们添加的WebContent目录删除掉,在右键工程–>properties–>Deployment Assembly中该删的删了,该添加的加上,最终下面这张图一样就差不多了。
    这里写图片描述
    这里配置的其实就是项目部署的目录,很明显,我们要把src/main/java和src/main/resource里面的文件要发布到项目的WEB-INF/classes目录里面,src/main/webapp下的文件发布到项目的/目录下面,当然maven Dependencies里面自动下载下来的jar包就要部署到项目的WEB-INF/lib目录下面了。

  5. 最后,同样的还得修改一下web.xml的声明和Dynamic Web Module版本对应,同上。修改完后别忘了右键工程–>Maven–>Update Project… 嗯!The END!


如何下载maven中依赖包的源码?

有些时候,遇到程序莫名其妙的错误的时候,可能需要去看源码找问题,如果不是maven工程,你可能需要一个一个的去下载jar包所对应的源码,然后再通过link关联起来,既麻烦也不优雅。但是如果是maven工程,这一切都好办,只要一个命令mvn dependency:sources就能解决。
右键工程–>Run As–>Maven build,在Goals中输入dependency:sources,再点击Run,然后等它下载完就行了。
这里写图片描述
so easy!

1 0