普通项目转为maven项目及相关操作说明
普通项目转为maven项目及相关操作说明
1 原项目简述

如图,一般的项目大致包括三类路径:src,源码路径;test,单元测试路径;lib第三方类包路径。
示例项目中,BaseDao类依赖于mysql-connector-java-5.1.15-bin.jar。SimTest类为依赖JUnit4的单元测试类。
2 将项目转为maven项目
2.1 添加pom.xml文件
在eclipse中邮件单击项目选择Maven->Enable Dependecy Management。如下图:

然后根据提示,设定maven项目信息。

之后生成的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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.sogou</groupId>
<artifactId>example.NormalToMaven</artifactId>
<version>0.0.1-SNAPSHOT</version>
</project>
2.2 向pom.xml中添加资源库和上传部署资源库
为了能够正确下载依赖的第三方jar包,需要在pom.xml中配置我们的资源库。添加如下配置:
<repositories>
<repository>
<id>sogou.repo</id>
<url>
http://***/nexus/content/groups/public
</url>
<name>Sogou Repository</name>
</repository>
<repository>
<id>sogou.repo.old</id>
<url>
http://***/nexus/content/groups/public
</url>
<name>Old Sogou Repository</name>
</repository>
</repositories>
为了能将项目的jar包上传到我们的资源库并被其他maven项目依赖,需要在pom.xml中配置上传资源库。添加如下配置:
<distributionManagement>
<repository>
<id>sogou-wd-releases</id>
<name>Sogou Release Distribution Repository</name>
<url>
http://***/nexus/content/repositories/Release
</url>
</repository>
<snapshotRepository>
<id>sogou-wd-snapshot</id>
<name>Sogou Snapshot Repository</name>
<url>
http://***/nexus/content/repositories/snapshots
</url>
</snapshotRepository>
</distributionManagement>
为了保证项目的jar包能正确上传,还需要确定本地maven的配置文件settings.xml中有对应上传资源库的用户名和密码。其配置如下:
<servers>
<server>
<id>sogou-wd-releases</id>
<username>你的用户名</username>
<password>你的密码</password>
</server>
<server>
<id>sogou-wd-snapshot</id>
<username>你的用户名</username>
<password>你的密码</password>
</server>
</servers>
其中id要与之前配置的上传资源库id对应。默认为LDAP中的用户名(不带@及之后的用户名)和密码,如果无效可以找谭博侃申请。
此外在settings.xml中可以配置我们的资源库为mirror,以我们的资源库为所有远程资源库的镜像,加快jar包的下载。其配置如下:
<mirrors>
<mirror>
<id>nexus-sogou</id>
<mirrorOf>*</mirrorOf>
<url>
http://***/nexus/content/groups/public
</url>
</mirror>
</mirrors>
当包括其他的所有配置都完成后,执行mvn clean deploy成功后,会在http://cloud.sogou-inc.com/nexus/中看到刚刚上传的jar包。如图:

2.3 向pom.xml中添加依赖jar包
以本项目为例,其配置如下:
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.16</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<type>jar</type>
<scope>test</scope>
</dependency>
</dependencies>
如果不知道依赖的第三方jar包的配置,可在http://***/nexus/ 中搜索。如下图:

2.4 配置源码和测试路径
由于一般在eclipse中开发的项目路径都不符合maven默认源码和测试路径,因此要对源码路径进行配置,否则maven不会进行编译和测试。以本项目为例,其配置如下:
<build>
<testSourceDirectory>test</testSourceDirectory>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.4</version>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>src</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apahce.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.5</version>
<configuration>
<testClassesDirectory>
target/classes-test
</testClassesDirectory>
<testSourceDirectory>test</testSourceDirectory>
<includes>
<include>**/*.java</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
其中插件org.codehaus.mojo. build-helper-maven-plugin用于添加源码路径。testSourceDirectory 和org.apahce.maven.plugins. maven-surefire-plugin用于指定测试源码路径。
2.5 其他配置
<properties>
<project.build.sourceEncoding>
GBK
</project.build.sourceEncoding>
<compileSource>1.6</compileSource>
</properties>
其中project.build.sourceEncoding代表项目中源码文件所使用的编码。compileSource代表编译版本。
3 web项目的maven配置
Web项目与示例项目不同,一方面需要打成war包;另一方面在转换成maven项目后,打包时需要将不同文件拷贝到不同路径下,这也是我们原有的项目不符合maven web项目文件路径的标准造成的。因此,除了之上的配置外还须一下配置。
3.1 配置打包
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1-beta-1</version>
<configuration>
<webXml>WebContent/WEB-INF/web.xml</webXml>
<archive>
<addMavenDescriptor>
false
</addMavenDescriptor>
</archive>
<webResources>
<resource>
<directory>src</directory>
<includes>
<include>**/struts.xml</include>
</includes>
<filtering>true</filtering>
<targetPath>WEB-INF/classes</targetPath>
</resource>
<resource>
<directory>WebContent</directory>
<filtering>true</filtering>
<targetPath>.</targetPath>
</resource>
</webResources>
</configuration>
</plugin>
其中webXml指定了web.xml文件所在的文件夹,如果插件无法找到web.xml则会打包失败。webResources表示在打包时资源文件夹需要移动到的指定目录。每个resource中directory代表资源文件原路径,可以用includes和excludes对文件进行过滤。targetPath为移动到的目录。
3.2 配置clean
Web项目中,需要额外清理WebContent/WEB-INF中的内容。对此,其配置如下:
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<configuration>
<filesets>
<fileset>
<directory>
WebContent/WEB-INF
</directory>
<includes>
<include>lib</include>
<include>classes</include>
</includes>
<followSymlinks>false</followSymlinks>
</fileset>
</filesets>
</configuration>
</plugin>
普通项目转为maven项目及相关操作说明的更多相关文章
- 如何用Eclipse将普通的JavaWeb项目转为Maven项目
最新自己的第一个项目差不多稳定运行之后 想着将项目转为Maven项目.于是参考网上成功的将自己的普通的项目转为了maven项目,现在记录一下: 0.普通的java项目的结构如下: 1.接下来开始进行正 ...
- Eclipse使用之将Git项目转为Maven项目, ( 注意: 最后没有pom.xml文件的, 要转化下 )
Eclipse使用之将Git项目转为Maven项目(全图解) 2017年08月11日 09:24:31 阅读数:427 1.打开Eclipse,File->Import 2.Git->Pr ...
- web项目转为maven项目
声明一下项目本来就是maven项目,只是刚开始部署的时候转为maven项目!!! 2.查看POM文件 3.导入依赖jar包(编译,运行,打包) 4. 注意项目为Maven+java 加载jar包小技巧
- 一般项目转为Maven项目所遇到的问题
最近搞CI,准备使用Maven,但以前的项目不是Maven项目,需要把项目转换为Maven项目.这遇到几个小问题,一是jar包的依赖,二是从本地仓库取出依赖jar包. 由于没有本地仓库,要手动添加ja ...
- 老项目转为maven的步骤具体说明
可先阅读 关于已有项目转为maven的一点看法 新建maven项目要点 事实上之前已转过几个.但忘了记录下来.今天又转了一个项目,补记录一下. 步骤 1.写pom.xml 最耗费时间的一步.由于不用m ...
- 如何把maven项目转为eclipse项目
如何把maven项目转为eclipse项目,按照如下操作便可. 在cmd窗口, 载cmd窗口进入到maven项目所在目录下,输入如下命令: mvn eclipse:eclipse 这样便可.
- eclipse web项目转maven项目
ps:好久没写博客了,工作了人就懒了,加油加油,up,up 1 eclipse web项目目录 /web app src com.xx.xx *.properties *.xml WebRoot W ...
- 将Maven2项目转为MyEclipse项目
现在项目中,大家开始用jetty.它不用像在MyEclipse中使用Tomcat那样要部署,也不用像在Tomcat中那样,要把应用都放到webapp文件夹下.jetty可以直接用你的项目的目录结构. ...
- 普通 Java 项目转换为 Maven 项目
普通 Java 项目转换为 Maven 项目 本文为原创文章,转载请注明出处.源码已分享至GitHub. 本文提供一个完整可行的将遗留项目转换为Maven项目的步骤.至于转换Maven构建项目的好处不 ...
随机推荐
- 02-cookie案例-显示用户上次访问网站的时间
package cookie; import java.io.IOException;import java.io.PrintWriter;import java.util.Date; import ...
- Nginx 经验小结
chmod 777 永远不要 使用 777,有时候可以懒惰的解决权限问题, 但是它同样也表示你没有线索去解决权限问题,你只是在碰运气. 你应该检查整个路径的权限,并思考发生了什么事情. 把 root ...
- 图像处理之log---log算子
在图像中,边缘可以看做是位于一阶导数较大的像素处,因此,我们可以求图像的一阶导数来确定图像的边缘,像sobel算子等一系列算子都是基于这个思想的. 但是这存在几个问题:1. 噪声的影响,在噪声点处一阶 ...
- COGS1752. [BOI2007]摩基亚Mokia
1752. [BOI2007]摩基亚Mokia ★★☆ 输入文件:mokia.in 输出文件:mokia.out 简单对比时间限制:5 s 内存限制:128 MB [题目描述] 摩尔瓦 ...
- 高性能流媒体服务器EasyDSS前端重构(四)- webpack + video.js 打造流媒体服务器前端
接上篇 接上篇<高性能流媒体服务器EasyDSS前端重构(三)- webpack + vue + AdminLTE 多页面引入 element-ui> 本文围绕着实现EasyDSS高性能流 ...
- android菜鸟学习笔记15----Android Junit测试
Android中的Junit测试与Java Junit测试有所不同,不能简单的使用标注…… 假设写了一个MathUtils类,有两个静态方法: public class MathUtils { pub ...
- 【spring配置】——spring整合Quartz定时器
第一种:为普通java类中的某个方法配置跑批任务 MethodInvokingJobDetailFactoryBean CronTriggerBean SchedulerFactoryBean 1.定 ...
- 《Linux 鸟哥私房菜》 第一部分 Linux文件、目录与磁盘格式
1.Linux就是内核层与系统调用接口层这2层.
- 如何使用RadioGroup和RadioButton实现FragmentTabHost导航效果?
目录: 一.概述 最近在做一个新闻类结合社区的APP的时候,需要添加一个侧滑菜单的效果,考虑到可以使用DrawerLayout布局,但是问题是使用了 DrawerLayout布局后,主页内容应该是一个 ...
- Yii 框架 URL路径简化
Yii 框架的訪问地址若不简化会让人认为非常繁琐.未简化的地址一般格式例如以下: http://localhost:80/test/index.php?r=xxx/xxx/xxx 若是带有參数会更复杂 ...