普通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的根节点声明设置为最新版本,如:
- <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">
b. 在pom.xml文件中加入maven的资源和编译插件等,配置参数:
- <build>
- <finalName>perfume</finalName>
- <plugins>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-resources-plugin</artifactId>
- <version>2.5</version>
- </plugin>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-compiler-plugin</artifactId>
- <version>2.3.2</version>
- <configuration>
- <source>1.7</source>
- <target>1.7</target>
- </configuration>
- </plugin>
- </plugins>
- </build>
注:
1. maven-compiler-plugin 2.3.2默认的JDK版本是1.5,后续版本新增的功能在编译时会报错(如注解等),解决办法是明确指定编译版本。
2. 在一个父工程包含多个子模块的项目中,在父pom.xml中添加编译配置,子模块将会继承。
c. 添加需要的依赖,如testng:
- <properties>
- <testng.version>6.4</testng.version>
- </properties>
- <dependencies>
- <dependency>
- <groupId>org.testng</groupId>
- <artifactId>testng</artifactId>
- <version>${testng.version}</version>
- <scope>test</scope>
- </dependency>
- </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
maven主要是用于解决包的依赖,一般来说 我们的工程都需要各种各样的包,通常放在WebRoot/WEB-INF/lib路径下
但是 我们每次 发布 工程 都需要 连包一起复制打包, 这样 就很不方便
maven能在资源库中 缓存 包,在一个服务器环境中除了第一次下载包,以后都可以直接进行读取
要实现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.reallyinfo</groupId>
- <artifactId>buildingProject</artifactId>
- <version>1.0.0-SNAPSHOT</version>
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- </properties>
- <build>
- <sourceDirectory>src/com</sourceDirectory>
- <resources>
- <resource>
- <directory>src</directory>
- <excludes>
- <exclude>**/*.java</exclude>
- </excludes>
- </resource>
- <resource>
- <directory>resource</directory>
- <excludes>
- <exclude>**/*.java</exclude>
- </excludes>
- </resource>
- </resources>
- <plugins>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-surefire-plugin</artifactId>
- <version>2.17</version>
- </plugin>
- <plugin>
- <groupId>org.codehaus.mojo</groupId>
- <artifactId>findbugs-maven-plugin</artifactId>
- <version>2.5.5</version>
- </plugin>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-checkstyle-plugin</artifactId>
- <version>2.13</version>
- <configuration>
- <configLocation>resource/checkstyle_checks.xml</configLocation>
- </configuration>
- </plugin>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-pmd-plugin</artifactId>
- <version>3.2</version>
- </plugin>
- </plugins>
- </build>
- <dependencies>
- <dependency>
- <groupId>jdk.tools</groupId>
- <artifactId>jdk.tools</artifactId>
- <version>1.6</version>
- <scope>system</scope>
- <systemPath>${JAVA_HOME}/lib/tools.jar</systemPath>
- </dependency>
- <dependency>
- <groupId>org.apache.httpcomponents</groupId>
- <artifactId>httpcore</artifactId>
- <version>4.2.4</version>
- </dependency>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>4.11</version>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>org.apache.hbase</groupId>
- <artifactId>hbase-client</artifactId>
- <version>0.98.0-hadoop2</version>
- </dependency>
- <dependency>
- <groupId>org.apache.hbase</groupId>
- <artifactId>hbase-common</artifactId>
- <version>0.98.0-hadoop2</version>
- </dependency>
- <dependency>
- <groupId>org.apache.hbase</groupId>
- <artifactId>hbase-server</artifactId>
- <version>0.98.0-hadoop2</version>
- </dependency>
- <dependency>
- <groupId>org.apache.struts.xwork</groupId>
- <artifactId>xwork-core</artifactId>
- <version>2.3.15.1</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-context</artifactId>
- <version>4.0.0.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>com.reallyinfo</groupId>
- <artifactId>aether</artifactId>
- <version>0.1</version>
- </dependency>
- <dependency>
- <groupId>com.reallyinfo</groupId>
- <artifactId>ezmorph</artifactId>
- <version>0.1</version>
- </dependency>
- <dependency>
- <groupId>com.reallyinfo</groupId>
- <artifactId>themis</artifactId>
- <version>0.1</version>
- </dependency>
- </dependencies>
- <reporting>
- <plugins>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-surefire-plugin</artifactId>
- <version>2.17</version>
- </plugin>
- <plugin>
- <groupId>org.codehaus.mojo</groupId>
- <artifactId>findbugs-maven-plugin</artifactId>
- <version>2.5.5</version>
- <configuration>
- <findbugsXmlOutput>true</findbugsXmlOutput>
- <findbugsXmlWithMessages>true</findbugsXmlWithMessages>
- <xmlOutput>true</xmlOutput>
- </configuration>
- </plugin>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-checkstyle-plugin</artifactId>
- <version>2.13</version>
- <configuration>
- <configLocation>resource/checkstyle_checks.xml</configLocation>
- </configuration>
- <reportSets>
- <reportSet>
- <reports>
- <report>checkstyle</report>
- </reports>
- </reportSet>
- </reportSets>
- </plugin>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-pmd-plugin</artifactId>
- <version>3.2</version>
- </plugin>
- </plugins>
- </reporting>
- </project>
如果我们只知道包的名字,不知道怎么写它的groupId,我们可以用MyEclipse中的自动搜索功能
在项目上点右键,依次选择Maven4MyEclipse-->add dependency,输入包名称,maven将从中央库查找。比如输入一个logg,就能找到所需要的commons-logging包,点击确定将添加到当前项目
或者去maven包的相关查找网站(有些通过MyEclipse来查找的包在中央仓库会下载失败,这种情况还是要在网站上查找):
pom.xml中配置的就是我要用到的包,以及它们的版本
然后在 myeclipse中 可以点击自动下载包
这样maven就会自动下载我们需要的包了 下载成功后 我们就可以把lib包删除了
如果我们用本地自己打的包,需要通过本地加载的方法
浙公网安备 33010602011771号