您的位置:首页 > 运维架构 > Tomcat

用Maven创建web项目(详细步骤)

2016-12-29 14:40 323 查看
首先,我们用eclipse创建一个Maven工程



我们勾选Creat a simple project 就是单纯的一个项目



Packaging选择war的形式



就会出现如下的目录,由于packing是war包,那么下面也就多出了webapp的目录



由于我们的项目要使用eclipse发布到tomcat下面,这里我们需要先把项目转成dynamic web project

在我们的项目上点击右键,选择properties 并找到 Project Facets ,并点击Convert to faceted form…





虽然此时我们可以发布到tomcat中,但这不符合maven的结构,我们还要做如下修改.把上图WebContent下面两个目录 META-INF ,WEB-INF 直接剪切到src/main/webapp目录下,并删掉WebContent目录,那么现在的项目结构如下图:



然后我们要修改发布规则,右键点击项目, 选择 Deployment Assembly

选择WebContent,把它remove掉,测试类我们也不需要发布,test的两个目录页可以remove



至此一个基于maven的webapp就建立好了,并可以直接从eclipse中发布到tomcat中,补充:我们需要在src/main/webapp/WEB-INF下面创建一个web.xml

然后导入依赖jar包

我这里说的是springmvc-spring-mybatis-0.1

springmvc-spring-mybatis-0.1

<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.hust.maven</groupId>
<artifactId>TestMavenWeb</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>

<name>Archetype - springmvc.spring.mybatis</name>
<description>
A pretty useful JavaEE application archetype based on Springmvc Spring and Mybatis
</description>
<url> http://rugal.ga/development/2014/07/06/my-archetype-in-maven/ </url>
<properties>
<!-- Using utf-8 encoding -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<developers>
<developer>
<name>Rugal Bernstein</name>
<email>rugal.bernstein.0@gmail.com</email>
</developer>
</developers>
<scm>
<connection>
scm:git:git@github.com:Rugal/springmvc-spring-mybatis.git
</connection>
<url>git@github.com:Rugal/springmvc-spring-mybatis.git</url>
<developerConnection>
scm:git:git@github.com:Rugal/springmvc-spring-mybatis.git
</developerConnection>
</scm>
<licenses>
<license>
<name>The Apache Software License, Version 2.0</name>
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
</license>
</licenses>
<distributionManagement>
<snapshotRepository>
<id>ossrh</id>
<url> https://oss.sonatype.org/content/repositories/snapshots </url>
</snapshotRepository>
<repository>
<id>ossrh</id>
<url> https://oss.sonatype.org/service/local/staging/deploy/maven2/ </url>
</repository>
</distributionManagement>
<build
ec51
>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<id>sign-artifacts</id>
<phase>verify</phase>
<goals>
<goal>sign</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.sonatype.plugins</groupId>
<artifactId>nexus-staging-maven-plugin</artifactId>
<version>1.6.2</version>
<extensions>true</extensions>
<configuration>
<serverId>ossrh</serverId>
<nexusUrl>https://oss.sonatype.org/</nexusUrl>
<autoReleaseAfterClose>false</autoReleaseAfterClose>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<version>2.5</version>
<configuration>
<autoVersionSubmodules>true</autoVersionSubmodules>
<useReleaseProfile>false</useReleaseProfile>
<releaseProfiles>release</releaseProfiles>
<goals>deploy</goals>
</configuration>
</plugin>
</plugins>
</build>

</project>


基本的Maven工程大功告成了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息