mavenWeb工程建立步骤

1、File >> New >>other...,在New窗口中打开Maven,选中Maven Project,Next。

2、在New Maven Project弹出窗口中去掉勾选Create a simple project,直接next。

3、在Filter中输入 “web”找到如下图中的选项,然后Next。注:此处quickstart选项即前面勾选Create a simple project创建的普通非mavenWeb项目,此处选webapp表示构建web项目。

 

4、填写项目名称信息。此处实际只要填写groupId和artifactId,packge名称会自动生成,就是groupId和artifactId的合并。填写完成后点击finish则创建成功。

groupId:group 翻译成中文就是组、集团的意识,groupId就是这样一个组或集团的ID标识,就拿 Apache 来说好了,Apache 大家都不陌生吧,Apache 官网上有非常多的项目,每个项目里面肯定能找得到 org.apache.xxxx,而 org.apache 就是这里说的 groupId

artifactId:还是拿 Apache 来说,Apache 官网上有非常多的项目,而每个项目的名字就是这里的 artifactId,换句话说,artifactId 就是你的项目名

 

5、新建好后的项目如果出现jsp文件的错误,一般是没有引用正确的Tomcat包文件。如图:

6、项目上右键,选择Properties,弹出如下窗口。选择左侧的菜单Maven >> Project Facets,在右侧选择runtimes,勾选Apache tomcat,然后录apply-->ok,则错误消除。

7、项目 >> 右键 >> Build Path >> New Source Folder,手动加入maven项目目录,如src/main/java,src/test/java,src/test/resources等。完整的项目目录图如下:

8、此时将项目加入到tomcat即可访问项目。访问路径localhost加端口号加项目名加文件名。如:http://localhost:8070/mavenlast/index.jsp。

注:tomcat端口号即启动时出现的HTTP/1.1 on http-8070对应的号码,此号码也可在双击server时看到,如下图:

9、maven的jetty启动方式(jetty和tomcat启动是两种不同的启动方式,性能特点不同,主流启动是tomcat):

(1)项目右键-->Run as-->Maven build (Alt+Shift+X,M),出现如下对话框,

    main标签下填写goals:jetty:run,然后勾选Skip Tests选项,

    jre标签下填写VM argument:-Xms1024m -Xmx1024m -XX:PermSize=256m -XX:MaxPermSize=384m

  点击apply-->run

(2)此时启动会报错,因为jetty启动需要相应的启动插件。在pom.xml文件中配置如下内容再启动即可。注:jetty启动访问时端口号是pom文件里配置的端口号,如下文件中配置的是8088;同时jetty启动访问时不用加项目名,不同项目识别都是通过端口号识别。如:http://localhost:8088/index.jsp。

<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.test</groupId>
  <artifactId>mavenlast</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>mavenlast Maven Webapp</name>
  <url>http://maven.apache.org</url>
  
  <properties>
        <maven.jetty.plugin.version>6.1.23</maven.jetty.plugin.version>
        <maven.release.plugin>2.1</maven.release.plugin>
        <project.build.sourceEncoding>GBK</project.build.sourceEncoding>
        <project.reporting.outputEncoding>GBK</project.reporting.outputEncoding>
        <servlet.version>2.5</servlet.version>
        <spring.version>3.0.7.RELEASE</spring.version>
        <ibatis.version>2.3.4.726</ibatis.version>
        <freemarker.version>2.3.16</freemarker.version>
        <aspectj.version>1.6.9</aspectj.version>
        
        <slf4j.verison>1.6.1</slf4j.verison>
        <log4j.version>1.2.16</log4j.version>
        <!--Test Related-->
        <junit.version>4.8.2</junit.version>
        <quartz.version>1.8.4</quartz.version>
        <jacksonVersion>1.9.9</jacksonVersion>
        <!-- cache -->
        <xmemcached.version>1.3.5</xmemcached.version>
        <httpcomponents.version>4.2</httpcomponents.version>
  </properties>
  
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <build>
    <finalName>mavenlast</finalName>
    <plugins>
        <plugin>
            <groupId>org.mortbay.jetty</groupId>
            <artifactId>maven-jetty-plugin</artifactId>
            <version>${maven.jetty.plugin.version}</version>
            <configuration>
                <contextPath>/</contextPath>
                <scanIntervalSeconds>3</scanIntervalSeconds>
                <systemProperties>
                    <systemProperty>
                        <name>CONFIG_DIR_PATH</name>
                        <value>${basedir}/config/config</value>
                    </systemProperty>
                       <systemProperty>
                           <name>org.mortbay.util.URI.charset</name>
                           <value>GBK</value>
                       </systemProperty>
                </systemProperties>
                <stopPort>9911</stopPort>
                <stopKey>foo</stopKey>
                <connectors>
                    <connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
                        <port>8088</port>
                        <maxIdleTime>60000</maxIdleTime>
                    </connector>
                </connectors>
            </configuration>
        </plugin>
    </plugins>
  </build>
</project>

 10、maven项目依赖包添加http://mvnrepository.com/

maven项目目录含义:

src/main/java application library sources - java源代码文件
src/main/resources application library resources - 资源库,会自动复制到classes文件夹下
src/main/filters resources filter files - 资源过滤文件
src/main/assembly assembly descriptor - 组件的描述配置,如何打包
src/main/config configuration files - 配置文件
src/main/webapp web application sources - web应用的目录,WEB-INF,js,css等
src/main/bin 脚本库
src/test/java 单元测试java源代码文件
src/test/resources 测试需要的资源库
src/test/filters 测试资源过滤库
src/site 一些文档
target/ 存放项目构建后的文件和目录,jar包,war包,编译的class文件等;Maven构建时生成的
pom.xml 工程描述文件
LICENSE.txt license
README.txt read me
原文地址:https://www.cnblogs.com/leskang/p/5434570.html