首先说下环境
我用的ECLIPSE:: Mars.2 Release (4.5.2)
我现在用的JDK 1.7 ,这也是我准备用于项目的JDK版本
我在网上下的apache-maven-3.5.0
首先,应该先配置maven的配置(文件),在maven的根目录的conf文件夹下面,有一个settings.xml的文件,讲几个主要的节点
<mirror> <id>nexus-aliyun</id> <mirrorOf>central</mirrorOf> <name>Nexus aliyun</name> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> </mirror> <mirror> <id>net-cn</id> <mirrorOf>central</mirrorOf> <name>Nexus net</name> <url>http://maven.net.cn/content/groups/public/</url> </mirror>
这是配置库镜像,因为maven的官方库在国外,我们就用阿里云的,不然下载很慢
<profiles> <profile> <id>custom-compiler</id> <properties> <JAVA8_HOME>:/usr/lib/jvm/java-7-oracle</JAVA8_HOME> </properties> </profile> <profile> <id>jdk-1.7</id> <activation> <activeByDefault>true</activeByDefault> <jdk>1.7</jdk> </activation> <properties> <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target> <maven.compiler.compilerVersion>1.7</maven.compiler.compilerVersion> </properties> <repositories> <repository> <id>nexus</id> <name>local private nexus</name> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>nexus</id> <name>local private nexus</name> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </pluginRepository> </pluginRepositories> </profile>
这是主要是配置创建maven工程时,工程默认用的jdk的版本及库位置
<activeProfiles> <activeProfile>nexus</activeProfile> <activeProfile>custom-compiler</activeProfile> </activeProfiles>
至此,maven的配置就可以了。然后再来配置eclipse。
第一步,调整eclipse所用的maven
由于某些原因,创建后有些目录将不会创建或者不显示,特别是resources目录
第二步,把刚刚我们配置的xml文件的配置刷新到eclipse中来,找到User Settings,定位到相关文件,然后Update Setting,然后,搞自己的库地址,Local Repository,想放哪里放哪时吧。
这样之后就可以创建maven工程了,有些要注意的事项就是,对于父工程,新建的时候类型选maven-archetype-quickstart,Group Id,即公司组织名,Artifact Id,即项目名。Package可以先删除不填。
父工程创建好以后,做几个事情
1、在properties->Resource中把编码调整为UTF8
2、由于某些原因,创建后有些目录将不会创建或者不显示,特别是resources目录,请做如下操作
3、将父工程的打包属性调整为POM
然后就可以在父工程中创建module了,需要注意的是,无论创建多少个子工程,都应该把刚刚的创建的父工程作为父工程(是不是废话,哈哈),另外,如果是创建普通的jar项目的话,
新建的时候类型选maven-archetype-quickstart,如果是web项目的话,新建的时候类型选maven-archetype-webapp,另外,创建完后以后,由于某些原因,创建后有些目录将不会创建或者不显示,特别是resources目录,请在创建的新的项目中重复上述的第2步操作。
然后,讲一下项目之类打包的注意细节
我现在的项目是spring-mvc,mybatis,spring项目,我按照http://juvenshun.iteye.com/blog/305865这篇文章,把services划分到一个层,这里面放dao,entity,mapper.xml等,其完整的pom.xml如下:
<?xml version="1.0"?> <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.baidu</groupId> <artifactId>ybh-parent-01</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <groupId>com.xiangyugame</groupId> <artifactId>ybh-service-01</artifactId> <version>0.0.1-SNAPSHOT</version> <name>ybh-service-01</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.dom4j</groupId> <artifactId>dom4j</artifactId> <version>2.0.1</version> </dependency> <dependency> <groupId>com.thoughtworks.xstream</groupId> <artifactId>xstream</artifactId> <version>1.4.3</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>3.0-alpha-1</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.1.1</version> </dependency> <dependency> <groupId>MysqL</groupId> <artifactId>MysqL-connector-java</artifactId> <version>5.1.19</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.4</version> </dependency> <dependency> <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> <version>2.4</version> </dependency> <dependency> <groupId>commons-validator</groupId> <artifactId>commons-validator</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>net.sf.json-lib</groupId> <artifactId>json-lib</artifactId> <version>2.4</version> <classifier>jdk15</classifier> </dependency> <dependency> <groupId>javax.annotation</groupId> <artifactId>jsr250-api</artifactId> <version>1.0</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.2</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.1.1</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> </dependencies> <build> <resources> <resource> <directory>src/main/java</directory> <includes> <include>com/accp/mappers/*.xml</include> </includes> <filtering>false</filtering> </resource> </resources> </build> </project>
其中后最后配置是的打包必须把*.xml打进去的
然后, 这个子模块的整个目录如下:
然后,最好先写一下测试这个子模块,src/test/java这个文件件下的AppTest.java就是放测试代理的地方
import static org.junit.Assert.*; import java.io.IOException; import java.io.InputStream; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.sqlSession; import org.apache.ibatis.session.sqlSessionFactory; import org.apache.ibatis.session.sqlSessionFactoryBuilder; import org.junit.Test; import com.accp.dao.UserDAO; import com.accp.entity.User; public class AppTest { @Test public void kkk() throws IOException{ InputStream inputStream = Resources.getResourceAsStream("mybatis.xml"); sqlSessionFactory sessionFactory = new sqlSessionFactoryBuilder().build(inputStream); sqlSession session = sessionFactory.openSession(); UserDAO userDAO = session.getMapper(UserDAO.class); User user = userDAO.find("o0-nWs46Vnoiy3w7Fyh1PmFxD6sg"); // assertTrue("true",user.equals("")); System.out.println(user); } }
然后在子模块上面右击,Run As->Maven test即可测试。
对于子web模块,其整个pom.xm如下:
<?xml version="1.0"?> <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.baidu</groupId> <artifactId>ybh-parent-01</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <groupId>com.xiangyugame</groupId> <artifactId>ybh-web-admin-01</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <name>ybh-web-admin-01 Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>com.xiangyugame</groupId> <artifactId>ybh-service-01</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>3.0-alpha-1</version> </dependency> <dependency> <groupId>ws-commons-util</groupId> <artifactId>ws-commons-util</artifactId> <version>1.0.1</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>taglibs</groupId> <artifactId>standard</artifactId> <version>1.1.2</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.4</version> </dependency> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.2.2</version> </dependency> <dependency> <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> <version>2.4</version> </dependency> <dependency> <groupId>commons-validator</groupId> <artifactId>commons-validator</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>net.sf.json-lib</groupId> <artifactId>json-lib</artifactId> <version>2.4</version> <classifier>jdk15</classifier> </dependency> <dependency> <groupId>javax.annotation</groupId> <artifactId>jsr250-api</artifactId> <version>1.0</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.2</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.1.1</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.1.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.1.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>4.1.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.1.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>4.1.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>4.1.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>4.1.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>4.1.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-oxm</artifactId> <version>4.1.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>4.1.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>4.1.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>4.1.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.1.6.RELEASE</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> </dependencies> <build> <finalName>ybh-web-admin-01</finalName> </build> </project>
目录结构如下:
原文链接:https://www.f2er.com/ubuntu/351969.html