最近在开发项目时用到maven,多个maven项目之间是有一定联系的,所以分开创建,再用maven聚合管理。
项目使用git来管理代码,因为上传代码时设置.gitignore文件中不上传.setting等文件,所以在git中下载下来之后的maven项目不是一个elipse项目文件,这样如果在github中拉下代码之后,再导入时不是很方便,所以这里使用maven的插件,把各个项目变成Eclipse项目。
项目的结构如下:
其中encryption是多个maven项目中的其中一个,这些项目都依赖Utils4Java-parent中的pom文件
Utils4Java-parent:
encryption项目(新建的其他maven项目结构也如此):
在这里除了pom文件之外其他项目没有打勾,是因为用git上传到github时在.gitignore文件中被忽略掉了,而src没有被上传是因为文件夹中我没有加源码,是空的。在这里可以看到eclipse项目文件标志的.setting没有被上传。
在eclipse中导入是这样的:
一.如何用maven管理多个项目
Utils4Java-parent的pom文件如下,所有的maven项目的pom文件都依赖这个父文件:
<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.kxw</groupId>
<artifactId>Utils4Java-parent</artifactId>
<packaging>pom</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>Utils4Java-parent</name>
<url>http://maven.apache.org</url>
<modules>
<module>../encryption</module>
</modules>
<profiles>
<profile>
<id>run_build</id>
<properties>
<log4j.print.sys>false</log4j.print.sys>
</properties>
</profile>
<profile>
<id>dev_build</id>
<properties>
<log4j.print.sys>true</log4j.print.sys>
</properties>
</profile>
</profiles>
<repositories>
<repository>
<id>central</id>
<name>Central Repository</name>
<url>https://nexus.sourcesense.com/nexus/content/repositories/public/</url>
<layout>default</layout>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<junit.version>4.10</junit.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<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>
<showWarnings>true</showWarnings>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.1.2</version>
<executions>
<execution>
<goals>
<goal>jar</goal>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.8</version>
<configuration>
<wtpversion>2.0</wtpversion>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<archive>
<manifestEntries>
<Vendor>${project.organization.name}</Vendor>
<Artifact-Id>${project.artifactId}</Artifact-Id>
<Implementation-Version>${project.version}</Implementation-Version>
<Build>${project.build}</Build>
</manifestEntries>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
encryption项目中的pom文件如下:
<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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.kxw</groupId>
<artifactId>Utils4Java-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../Utils4Java-parent/pom.xml</relativePath>
</parent>
<artifactId>encryption</artifactId>
<packaging>jar</packaging>
<name>encryption</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
这样就可以在这基础上新建多个maven文件并依赖于父POM文件了。
二.用git管理项目代码
.gitignore文件:
.*.swp
.DS_Store
*target*
*.jar
*.war
*.ear
*.class
classes/
.svn
.classes
.project
.classpath
.settings/
.metadata
bin
tmp/**
tmp/**/*
*.tmp
*.bak
*.swp
*~.nib
.classpath
.settings/**
tartget/**
git.properties
想了解如何用git上传代码可以看这里:http://blog.csdn.net/kingson_wu/article/details/38436923
三.使用maven插件把项目变成eclipse项目
这些插件在父POM文件已经配置,只有运行MavenEclipseBuild.bat即可
cd Utils4Java-parent
call mvn eclipse:clean eclipse:eclipse
pause
四.使用maven打包开发(测试)环境和正式环境
mavenProjectBuild.bat正式
cd Utils4Java-parent
call mvn clean install -Dmaven.test.skip=true -Prun_build
pause
mavenProjectBuild-Dev.bat开发
cd Utils4Java-parent
call mvn clean install -Dmaven.test.skip=true -Pdev_build
pause
parent的pom中:
<profiles>
<profile>
<id>run_build</id>
<properties>
<log4j.print.sys>false</log4j.print.sys>
</properties>
</profile>
<profile>
<id>dev_build</id>
<properties>
<log4j.print.sys>true</log4j.print.sys>
</properties>
</profile>
</profiles>
-------------
重新pull代码之后
先project build
再Eclipse build
然后 project clean(在Eclipse中)
注意:切完 分支之后要重新eclipse.bat,否则它的依赖是旧的。