普通Java工程转换成maven工程

以下内容转自:http://czj4451.iteye.com/blog/1983889

普通Java工程转换成maven工程

一、使用IDE提供的转换功能: 

1. 右键普通Java项目,在弹出的菜单中选择[Configure]-[Convert to Maven Project]: 
 

2. 在弹出的对话框中输入项目的groupId, artifactId和version等,点击[Finish],在根目录下生成maven的pom.xml文件。 

3. 经过IDE转换的maven项目还不能满足要求,需要手动配置: 

a. 将pom.xml的根节点声明设置为最新版本,如: 

Xml代码  收藏代码
  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">  



b. 在pom.xml文件中加入maven的资源和编译插件等,配置参数: 

Xml代码  收藏代码
  1. <build>  
  2.   <finalName>perfume</finalName>  
  3.   <plugins>  
  4.       <plugin>  
  5.           <groupId>org.apache.maven.plugins</groupId>  
  6.           <artifactId>maven-resources-plugin</artifactId>  
  7.           <version>2.5</version>  
  8.       </plugin>  
  9.       <plugin>  
  10.           <groupId>org.apache.maven.plugins</groupId>  
  11.           <artifactId>maven-compiler-plugin</artifactId>  
  12.           <version>2.3.2</version>  
  13.           <configuration>  
  14.               <source>1.7</source>  
  15.               <target>1.7</target>  
  16.           </configuration>  
  17.       </plugin>  
  18.   </plugins>  
  19. </build>  


注: 
1. maven-compiler-plugin 2.3.2默认的JDK版本是1.5,后续版本新增的功能在编译时会报错(如注解等),解决办法是明确指定编译版本。 

2. 在一个父工程包含多个子模块的项目中,在父pom.xml中添加编译配置,子模块将会继承。 


c. 添加需要的依赖,如testng: 

Xml代码  收藏代码
  1. <properties>  
  2.   <testng.version>6.4</testng.version>  
  3. </properties>  
  4.   
  5. <dependencies>  
  6.   <dependency>  
  7.     <groupId>org.testng</groupId>  
  8.     <artifactId>testng</artifactId>  
  9.     <version>${testng.version}</version>  
  10.     <scope>test</scope>  
  11.   </dependency>  
  12. </dependencies>  



d. 在项目根目录新建4个源码文件夹: 
   src/main/java - 存放类 
   src/main/resources - 存放配置等资源 
   src/test/java - 存放测试类 
   src/test/resources - 存放测试配置等资源 

e. 这时项目可能会有红色提示符,需要更新Maven配置: 
 

  右键项目,在弹出的菜单中选择[Maven]-[Update Project Configuration...],然后确定。 

这样,一个完整的maven工程算是转换好了。 

二、手动方式 

1. 手动创建或拷贝其它maven项目的pom.xml文件,修改参数 

2. 点击[Configure]-[Convert to Maven Project],这时就不需要输入groupId和artifactId等参数了。 

 

 

 

以下内容转自:http://blog.csdn.net/zzq900503/article/details/40590489

myeclipse中运用maven自动下载包

maven主要是用于解决包的依赖,一般来说 我们的工程都需要各种各样的包,通常放在WebRoot/WEB-INF/lib路径下

 

但是 我们每次 发布 工程 都需要 连包一起复制打包, 这样 就很不方便 

 

maven能在资源库中 缓存 包,在一个服务器环境中除了第一次下载包,以后都可以直接进行读取

 

要实现maven对包 管理  首先 要 有配置文件  pom.xml

 

我的配置文件如下:

 

[java] view plain copy
 
  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/xsd/maven-4.0.0.xsd">  
  3.     <modelVersion>4.0.0</modelVersion>  
  4.     <groupId>com.reallyinfo</groupId>  
  5.     <artifactId>buildingProject</artifactId>  
  6.     <version>1.0.0-SNAPSHOT</version>  
  7.     <properties>  
  8.         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
  9.     </properties>  
  10.     <build>  
  11.         <sourceDirectory>src/com</sourceDirectory>  
  12.         <resources>  
  13.             <resource>  
  14.                 <directory>src</directory>  
  15.                 <excludes>  
  16.                     <exclude>**/*.java</exclude> 
  17.                 </excludes> 
  18.             </resource> 
  19.             <resource> 
  20.                 <directory>resource</directory> 
  21.                 <excludes> 
  22.                     <exclude>**/*.java</exclude>  
  23.                 </excludes>  
  24.             </resource>  
  25.         </resources>  
  26.         <plugins>  
  27.             <plugin>  
  28.                 <groupId>org.apache.maven.plugins</groupId>  
  29.                 <artifactId>maven-surefire-plugin</artifactId>  
  30.                 <version>2.17</version>  
  31.             </plugin>  
  32.             <plugin>  
  33.                 <groupId>org.codehaus.mojo</groupId>  
  34.                 <artifactId>findbugs-maven-plugin</artifactId>  
  35.                 <version>2.5.5</version>  
  36.             </plugin>  
  37.             <plugin>  
  38.                 <groupId>org.apache.maven.plugins</groupId>  
  39.                 <artifactId>maven-checkstyle-plugin</artifactId>  
  40.                 <version>2.13</version>  
  41.                 <configuration>  
  42.                     <configLocation>resource/checkstyle_checks.xml</configLocation>  
  43.                 </configuration>  
  44.             </plugin>  
  45.             <plugin>  
  46.                 <groupId>org.apache.maven.plugins</groupId>  
  47.                 <artifactId>maven-pmd-plugin</artifactId>  
  48.                 <version>3.2</version>  
  49.             </plugin>  
  50.         </plugins>  
  51.     </build>  
  52.     <dependencies>  
  53.         <dependency>  
  54.             <groupId>jdk.tools</groupId>  
  55.             <artifactId>jdk.tools</artifactId>  
  56.             <version>1.6</version>  
  57.             <scope>system</scope>  
  58.             <systemPath>${JAVA_HOME}/lib/tools.jar</systemPath>  
  59.         </dependency>  
  60.         <dependency>  
  61.             <groupId>org.apache.httpcomponents</groupId>  
  62.             <artifactId>httpcore</artifactId>  
  63.             <version>4.2.4</version>  
  64.         </dependency>  
  65.         <dependency>  
  66.             <groupId>junit</groupId>  
  67.             <artifactId>junit</artifactId>  
  68.             <version>4.11</version>  
  69.             <scope>test</scope>  
  70.         </dependency>        
  71.         <dependency>  
  72.             <groupId>org.apache.hbase</groupId>  
  73.             <artifactId>hbase-client</artifactId>  
  74.             <version>0.98.0-hadoop2</version>  
  75.         </dependency>  
  76.         <dependency>  
  77.             <groupId>org.apache.hbase</groupId>  
  78.             <artifactId>hbase-common</artifactId>  
  79.             <version>0.98.0-hadoop2</version>  
  80.         </dependency>  
  81.         <dependency>  
  82.             <groupId>org.apache.hbase</groupId>  
  83.             <artifactId>hbase-server</artifactId>  
  84.             <version>0.98.0-hadoop2</version>  
  85.         </dependency>  
  86.         <dependency>  
  87.             <groupId>org.apache.struts.xwork</groupId>  
  88.             <artifactId>xwork-core</artifactId>  
  89.             <version>2.3.15.1</version>  
  90.         </dependency>  
  91.         <dependency>  
  92.             <groupId>org.springframework</groupId>  
  93.             <artifactId>spring-context</artifactId>  
  94.             <version>4.0.0.RELEASE</version>  
  95.         </dependency>  
  96.         <dependency>  
  97.             <groupId>com.reallyinfo</groupId>  
  98.             <artifactId>aether</artifactId>  
  99.             <version>0.1</version>  
  100.         </dependency>  
  101.         <dependency>  
  102.             <groupId>com.reallyinfo</groupId>  
  103.             <artifactId>ezmorph</artifactId>  
  104.             <version>0.1</version>  
  105.         </dependency>  
  106.         <dependency>  
  107.             <groupId>com.reallyinfo</groupId>  
  108.             <artifactId>themis</artifactId>  
  109.             <version>0.1</version>  
  110.         </dependency>  
  111.     </dependencies>  
  112.     <reporting>  
  113.         <plugins>  
  114.             <plugin>  
  115.                 <groupId>org.apache.maven.plugins</groupId>  
  116.                 <artifactId>maven-surefire-plugin</artifactId>  
  117.                 <version>2.17</version>  
  118.             </plugin>  
  119.             <plugin>  
  120.                 <groupId>org.codehaus.mojo</groupId>  
  121.                 <artifactId>findbugs-maven-plugin</artifactId>  
  122.                 <version>2.5.5</version>  
  123.                 <configuration>  
  124.                     <findbugsXmlOutput>true</findbugsXmlOutput>  
  125.                     <findbugsXmlWithMessages>true</findbugsXmlWithMessages>  
  126.                     <xmlOutput>true</xmlOutput>  
  127.                 </configuration>  
  128.             </plugin>  
  129.             <plugin>  
  130.                 <groupId>org.apache.maven.plugins</groupId>  
  131.                 <artifactId>maven-checkstyle-plugin</artifactId>  
  132.                 <version>2.13</version>  
  133.                 <configuration>  
  134.                     <configLocation>resource/checkstyle_checks.xml</configLocation>  
  135.                 </configuration>  
  136.                 <reportSets>  
  137.                     <reportSet>  
  138.                         <reports>  
  139.                             <report>checkstyle</report>  
  140.                         </reports>  
  141.                     </reportSet>  
  142.                 </reportSets>  
  143.             </plugin>  
  144.             <plugin>  
  145.                 <groupId>org.apache.maven.plugins</groupId>  
  146.                 <artifactId>maven-pmd-plugin</artifactId>  
  147.                 <version>3.2</version>  
  148.             </plugin>  
  149.         </plugins>  
  150.     </reporting>  
  151. </project>  

 

 

如果我们只知道包的名字,不知道怎么写它的groupId,我们可以用MyEclipse中的自动搜索功能

 

在项目上点右键,依次选择Maven4MyEclipse-->add dependency,输入包名称,maven将从中央库查找。比如输入一个logg,就能找到所需要的commons-logging包,点击确定将添加到当前项目

 

 

 

或者去maven包的相关查找网站(有些通过MyEclipse来查找的包在中央仓库会下载失败,这种情况还是要在网站上查找):

http://www.findmaven.net/

http://mvnrepository.com/

http://search.maven.org/

 

 

 

pom.xml中配置的就是我要用到的包,以及它们的版本

 

然后在 myeclipse中 可以点击自动下载包 

 

这样maven就会自动下载我们需要的包了  下载成功后 我们就可以把lib包删除了 

 

如果我们用本地自己打的包,需要通过本地加载的方法 

posted on 2017-06-18 08:40  傻瓜乐园  阅读(943)  评论(0)    收藏  举报

导航