1、创建Web项目
添加Maven支持
2、pom.xml 报如下错误:
解决办法:
pom.xml里面添加依赖:
<dependency> <groupId>com.thoughtworks.xstream</groupId> <artifactId>xstream</artifactId> <version>1.4.7</version> </dependency>
重启Myeclipse, 在项目上 右键->maven4Myeclipse->update project Configrotion->勾选force update of……即可;
3、补全目录
项目右击-> new -> source folder 建立如下目录结构 resources下面一般放配置文件;
4、修改一下 index.jsp文件,改为 Hello Maven!
5、部署 在server视图中 点击如下按钮
6、启动项目,在浏览器中输入:http://localhost:8080/helloMaven/ 即可!
7、下面的开发工作与Web开发大体相同,区别在于以下两点:
① 引入jar包时,不需要收工引入,在pom.xml添加依赖即可
② 测试相关的放在test目录下,代码相关的放在Java目录下,配置相关的放在resource下面
8、一个简单示例项目结构如下:
9、下面介绍一些常用的命令
Maven test --运行项目中的测试代码,在正式运行之前,可能需要下载很多其他文件,需要等待一些时间
Maven build... ---执行Maven命令,在弹出的对话框中 的Goals里面输入要执行的命令,点击run即可执行命令。
Maven install ------将项目输出构件部署到本地仓库
Maven clean ------将target里面的class类清除,但配置文件不会清除
myeclipse中执行Maven命令:
选择 Maven build ...... 点击select
10.Maven 远程发布到中央仓库
首先配置鉴权(安全认证),在本地setting.xml文件的servers标签下面添加:
<server> <id>releases</id> <username>admin</username> <password>admin123</password> </server> <server> <id>snapshots</id> <username>admin</username> <password>admin123</password> </server>
其中:id 是仓库名,使用username和userpassword登录后,点击左侧的Repositories即可看到;
项目配置,修改pom.xml文件
<distributionManagement> <repository> <id>releases</id> <!-- 与setting.xml中的server id对应 --> <name>panteng_release</name> <!-- 此URL在Repositories可以看到 --> <url>http://192.168.63.133:9434/nexus/content/repositories/releases/</url> </repository> </distributionManagement>
run - build... - deploy命令即可;
如果返回401 一般是用户权限设置错误,在setting.xml中配置用户权限
返回400 可能是不允许部署,进行如下操作:
或者可能是pom.xml中的version标签包含了 0.0.1-SNAPSHOT,把 -SNAPSHOT去掉即可(http://www.codeweblog.com/maven-deploy-return-code-is-400)
1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 2 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 3 <modelVersion>4.0.0</modelVersion> 4 <groupId>com.hi19pay.comm.ctp</groupId> 5 <artifactId>demo-parent</artifactId> 6 <version>0.0.1-SNAPSHOT</version> 7 <packaging>pom</packaging> 8 9 <name>demo-parent</name> 10 <description>19pay comm demo-parent project</description> 11 12 <licenses> 13 <license> 14 <name>19pay CTPI parent</name> 15 <url>http://www.19pay.com.cn</url> 16 <distribution>repo</distribution> 17 <comments>A business-friendly OSS license</comments> 18 </license> 19 </licenses> 20 21 <organization> 22 <name>hi19pay</name> 23 <url>http://www.19pay.com.cn</url> 24 </organization> 25 26 <developers> 27 <developer> 28 <name>jiangzx</name> 29 <organization>hi19pay</organization> 30 <organizationUrl>//www.19pay.com.cn</organizationUrl> 31 <roles> 32 <role>comm developer</role> 33 </roles> 34 <email>jiangzx@19pay.com.cn</email> 35 </developer> 36 </developers> 37 38 <modules> 39 <module>demo-util</module> 40 <module>demo-entity</module> 41 <module>demo-dao</module> 42 <module>demo-service</module> 43 <module>demo-web</module> 44 </modules> 45 46 <properties> 47 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 48 <springversion>3.0.1.RELEASE-A</springversion> 49 <junitversion>4.12</junitversion> 50 </properties> 51 52 <build> 53 <pluginManagement> 54 <plugins> 55 <!-- 编译源代码 --> 56 <plugin> 57 <groupId>org.apache.maven.plugins</groupId> 58 <artifactId>maven-compiler-plugin</artifactId> 59 <version>3.3</version> 60 <configuration> 61 <source>1.6</source> 62 <target>1.6</target> 63 <compilerArguments> 64 <!-- 指定外部lib路径 --> 65 <extdirs>${project.basedir}/lib</extdirs> 66 </compilerArguments> 67 </configuration> 68 </plugin> 69 70 <!-- 打包源代码 --> 71 <plugin> 72 <groupId>org.apache.maven.plugins</groupId> 73 <artifactId>maven-source-plugin</artifactId> 74 <version>3.0.1</version> 75 <executions> 76 <execution> 77 <id>attach-sources</id> 78 <goals> 79 <goal>jar</goal> 80 </goals> 81 </execution> 82 </executions> 83 </plugin> 84 85 <!-- 生成javadoc --> 86 <plugin> 87 <groupId>org.apache.maven.plugins</groupId> 88 <artifactId>maven-javadoc-plugin</artifactId> 89 <version>2.10.4</version> 90 <executions> 91 <execution> 92 <id>attach-javadocs</id> 93 <goals> 94 <goal>jar</goal> 95 </goals> 96 </execution> 97 </executions> 98 <configuration> 99 <aggregate>true</aggregate> <!-- 多模块工程中,将javadoc集中到父工程 --> 100 <encoding>UTF-8</encoding> 101 </configuration> 102 </plugin> 103 104 <!-- Test --> 105 <plugin> 106 <groupId>org.apache.maven.plugins</groupId> 107 <artifactId>maven-surefire-plugin</artifactId> 108 <version>2.18.1</version> 109 <configuration> 110 <skipTests>true</skipTests> <!-- 打包过程忽略Junit测试 --> 111 </configuration> 112 </plugin> 113 114 <!-- 将外部lib一起打成war包 --> 115 <plugin> 116 <groupId>org.apache.maven.plugins</groupId> 117 <artifactId>maven-war-plugin</artifactId> 118 <configuration> 119 <webResources> 120 <resource> 121 <directory>${project.basedir}/lib</directory> 122 <targetPath>WEB-INF/lib</targetPath> 123 <includes> 124 <include>**/*.jar</include> 125 </includes> 126 </resource> 127 </webResources> 128 </configuration> 129 </plugin> 130 131 <!-- 生成manifest文件--> 132 <plugin> 133 <groupId>org.apache.maven.plugins</groupId> 134 <artifactId>maven-jar-plugin</artifactId> 135 <version>2.3.1</version> 136 <configuration> 137 <archive> 138 <manifest> 139 <addClasspath>true</addClasspath> <!-- 依赖的jar--> 140 </manifest> 141 </archive> 142 </configuration> 143 </plugin> 144 145 <!-- mybatis代码生成工具 --> 146 <plugin> 147 <groupId>org.mybatis.generator</groupId> 148 <artifactId>mybatis-generator-maven-plugin</artifactId> 149 <version>1.3.2</version> 150 <configuration> 151 <verbose>true</verbose> <!--允许移动生成的文件--> 152 <overwrite>true</overwrite> <!--允许覆盖生成的文件--> 153 </configuration> 154 </plugin> 155 156 </plugins> 157 </pluginManagement> 158 </build> 159 160 <distributionManagement> 161 <repository> 162 <id>releases</id> 163 <name>Internal Releases</name> 164 <url>http://192.168.63.133:9434/nexus/content/repositories/releases/ 165 </url> 166 </repository> 167 <snapshotRepository> 168 <id>snapshots</id> 169 <name>Internal Snapshots</name> 170 <url>http://192.168.63.133:9434/nexus/content/repositories/snapshots/ 171 </url> 172 </snapshotRepository> 173 </distributionManagement> 174 175 <dependencyManagement> 176 <dependencies> 177 <dependency> 178 <groupId>javax.servlet</groupId> 179 <artifactId>javax.servlet-api</artifactId> 180 <version>3.0.1</version> 181 </dependency> 182 183 <dependency> 184 <groupId>javax.servlet.jsp</groupId> 185 <artifactId>jsp-api</artifactId> 186 <version>2.2</version> 187 </dependency> 188 189 <dependency> 190 <groupId>javax.servlet</groupId> 191 <artifactId>jstl</artifactId> 192 <version>1.2</version> 193 </dependency> 194 195 <dependency> 196 <groupId>org.slf4j</groupId> 197 <artifactId>slf4j-log4j12</artifactId> 198 <version>1.7.7</version> 199 </dependency> 200 201 <dependency> 202 <groupId>junit</groupId> 203 <artifactId>junit</artifactId> 204 <version>${junitversion}</version> 205 <scope>test</scope> 206 </dependency> 207 208 <dependency> 209 <groupId>org.apache.commons</groupId> 210 <artifactId>commons-lang3</artifactId> 211 <version>3.3.2</version> 212 </dependency> 213 214 <dependency> 215 <groupId>org.apache.commons</groupId> 216 <artifactId>commons-collections4</artifactId> 217 <version>4.0</version> 218 </dependency> 219 220 <!-- Apache Commons Codec --> 221 <dependency> 222 <groupId>commons-codec</groupId> 223 <artifactId>commons-codec</artifactId> 224 <version>1.10</version> 225 </dependency> 226 227 <dependency> 228 <groupId>org.dom4j</groupId> 229 <artifactId>dom4j</artifactId> 230 <version>2.0.0</version> 231 </dependency> 232 </dependencies> 233 </dependencyManagement> 234 </project>