Maven 构建web工程

前言: web开发 一直用tomcat ,maven 这个词很熟悉一直没用过,如今开始用maven构建一次工程。web

环境: jdk1.8   eclipse 4.6.1 maven3.3.9(jdk eclipse  maven3.3.9安装过程这里不讲解)apache

step1 :   eclipse 下配置 maven : window-preferences-Maven-Installations-add 把maven加载进来tomcat

再进入User-Settings ,在User Settings(open file):里引入maven的 settings.xmlapp

在maven安装目录下/conf/打开settings.xml,加入<localRepository>E:\workspaces\maven</localRepository> 这是maven加载组件的地方eclipse

step 2: 利用eclipse向导建一个maven工程maven

File-New-Other-Maven-Maven-Project  直接Next,出现ui

直接Next,出现this

选中 ...webpp 1.0 ,nexturl

Group Id 写的包结构 com.xx ,Artifact Id写上项目名称就能够,点 "Finish",一个项目就建好了.spa

setp 3:  

项目会自动生成maven 的pom.xml,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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.zw</groupId>
  <artifactId>firstmaven</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>firstmaven Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <build>
 
    <finalName>firstmaven</finalName>
  </build>

</project>
maven根据pom.xml会自动加载须要的包,这时eclipse的状态栏会显示更新须要的jar, 这是一个漫长的过程,不少包是从国外的官方更新 ,效率很低. (我1秒更新不到1K,当时电脑开着更新了一夜,有一个更新卡住99%,还被我终止掉了,这可能致使了接下来的一个问题,后面会提到).

step4 :更新完后 pom.xml报一个错误:

  在<packaging>war</packaging>

提示:Multiple annotations found at this line:

略去一大段的描述,有一个细节将问题定位在E:\Workspaces\maven\org\apache\maven\plugins\maven-resources-plugin\2.6\maven-resources-plugin-2.6.jar,好像就是刚才更新99% 卡住那个包.

尝试1: 把2.6下内容全删掉,maven自动再更新,更新后无效。

尝试2: 找资料解决一下,找到的资料:

一样也是Multiple annotations found at this line:

It is not a unique issue, happens every now and then (sometimes due to a slow connection and sometimes due to proxy servers now allowing to download)

You can get rid of this by either of the following ways:

1) Force Update:Right Click on the Project in Eclipse -> Maven -> Update Project On this screen select the check box Force Update for Snapshots/Releases

2) Clearing Maven Cache:If you still face a problem, go to the local repository on your system, which might be present atC:\Users\myusername\.m2\repositoryand delete the .cache folder and then follow step 1.

If still facing issues, manually go to the org/apache folder and delete everything and then follow step 1. (This will definitely solve the issue

按照资料显示第一步第二步  force update 并 清了cache . 错误消失 。

资料的解释缘由是:It is not a unique issue, happens every now and then (sometimes due to a slow connection and sometimes due to proxy servers now allowing to download)

正是我下载时出现的问题。

step 5 : 运行 

eclipse -Run As- Maven... 直接Run 

build faulire 报No goal错误,

  <build>
 
    <finalName>firstmaven</finalName>
  </build>

加入 <defaultGoal>compile</defaultGoal>

  <build>
  <defaultGoal>compile</defaultGoal>
    <finalName>firstmaven</finalName>
  </build>

再Run一次,build successful!

Maven工程建完,先写到这儿.

相关文章
相关标签/搜索