最近在接手一个使用maven构建的java web项目,项目不大,但是分层非常细,项目分成五个小maven项目构建:core,domain,dao,biz,manager。在pop.xml中配置依赖关系。
接着给我产生的问题就来了:
1,web里面打开五个工程,每个工程里面都是maven的结构,感觉要寻找一个类挺累,左边树状导航栏基本上用不着了。
项目结构
--core
|--java(公共类)
|--resources(公共配置文件,集成定时器其他公共包)
--domain
|--java
|--功能A实体BEAN
|--功能B实体BEAN
|--功能C实体BEAN
--dao
|--java
|--功能A数据操作
|--功能B数据操作
|--功能C数据操作
--biz
|--java
|--功能A业务
|--功能B业务
|--功能C业务
--manager
|--java
|--功能A请求
|--功能B请求
|--功能C请求
|--resources
|--配置文件A
|--配置文件B
|--配置文件C
|--webapps
|--页面A
|--页面B
|--页面C
2,当部署manager项目时,会把biz,dao,domain,core都打包成一个标准的jar包,但在开发环境中,每次重新在Servers上Publishs项目,此时另外几个项目的jar包也会拷贝到lib里面,这样在调试时,代码会进入class文件,而不进入源码。
试想的解决方案:
1,因项目太乱,在开发中只保留manager项目,其他项目把src/main/java和src/main/resources引入到manager的claaapath下,引入绝对路径,这样就可以关闭掉其他工程,使项目整洁。
2,按网上的方法,把manager项目改成Dynamic Web Module,配置好相关信息,把工程所有信息编译到src/main/webapp下面。
总结(两个月后):
今天本来是想给这篇文章添加一个使用命令的, 但是发现现在对maven已经截然不同,所以准备从头开始记录下,并结合目前的理解解答下之前自己的疑问。
一、 前言
在大型项目中,版本管理和持续集成是关键,特别是在盛行敏捷开发的年代,每个月一个常规版本是必须的,平均起来质量好点的项目平均起来不要高于20天一个发型版本(紧急版本),maven的主要功能是管理构建每个版本的jar包、war包。项目架构越优模块化,就越能发扬版本迭代的优势。举个例子:以前我们做一个维护了七八年的大型mis系统(Management Information System),整个项目装到eclipse里面就一个工程,打开就巨卡,左边导航栏基本上是摆设,启动一下程序得花十分钟。所有的开发人员(大概50人)都在上面开发(七八个小组),每个月一个常规版本,我们成为常规分支,因为每个组的交付功能时间不同,所以不能走常规版本的小组便会申请一个分支。一般线上都会不少于8个分支,平均一周不到就发行一个分支版本(当时是用svn管理).
--主分支
--常规分支
--特殊分支1(teamA,teamB,teamC同时修改)
--特殊分支2
--特殊分支3
--特殊分支4
--XXX...
举个例子,下一个版本是发布特殊分支1的,这个分支上可能主要是发布teamA的功能,但是teamB和teamC也趁机修复了两个漏洞或补了两个小功能。发布完成之后,该分支会合到所有可用的其他分支里面(这个应该好理解,如下次发布特殊分支2的代码,但是特殊分支2上没有teamA、teamB、teamC的代码啊)。此时就可能会造成teamB和tearmC的代码和其他分支有冲突,所以第二天你来上班更新代码后,突然发现代码里面好多冲突,咋一看,都不是自己改的~~,当时就傻眼的,只能等别人先解决了冲突再启动程序进行编码测试(一般小半天就没了)
如果换成maven的模块化管理,六七个小组分成不同的模块工程,每个小组只需要下载自己需要修改的模块和相应的依赖模块(公共包或者核心包)。每个团队在发布完成之后再把自己的代码上传到私服(一般用于管理整个公司所有的模块),这样如果其他小组不需要依赖你的jar包的功能,那都可以不用更新,如需要,则再更新下maven的版本即可。也完全不会有的代码冲突的问题。
上面这个问题使用svn+lvy也是可以实现的。本人只是大致了解。
一、 使用
1,maven解压即可用,将maven/bin文件夹放入PATH环境变量下。
2,eclipse集成进去maven(大部分eclise都已经集成maven,如没有可百度一下)
3,meven下面源码, 一般我喜欢把源码也下载下来:eclipse--window--Preferences--Maven页面,勾选Download Artifact Sources4,添加自己的maven:eclipse--window--Preferences--Maven--Installations页面,点击Add按钮,添加第二步下载maven
5,添加settings文件,这个步骤比较重要。eclipse--window--Preferences--Maven--UserSettings,添加maven/conf/settings.xml
6,设置settings.xml文件,该文件是整个maven的全局设置,可以说是所有使用该maven的pom.xml文件的父类(会应用到所有管理的项目中)
<!-- 本地仓库保存的路径,本地缓存 -->
<localRepository>D:\tomcatSSO\nexus-2.13.0-01-bundle\native-repo</localRepository>
<!-- 私服:一般每个公司都会建立一个自己的私有服务仓库(私服)用于存放管理第三方jar和自己项目的jar,该设置是配置私服的地址,分为SNAPSHOP(开发人员都可上传)和RELEASE(发行版本,版本经理会项目经理才有权限上传)-->
<servers>
<server>
<id>helloword-snapshots</id>
<username>deployment</username>
<password>deployment123</password>
</server>
<server>
<id>helloword-release</id>
<username>deployment</username>
<password>deployment123</password>
</server>
</servers>
<!-- 控制所有的jar包都需要通过制定的镜像地址下载,便于统一管理 -->
<mirrors>
<mirror>
<id>mirrors-nexusId</id>
<mirrorOf>*</mirrorOf>
<name>Nexus Mirror.</name>
<url>http://localhost:8081/nexus/content/groups/public/</url>
</mirror>
</mirrors>
<!-- 配置profile,类是spring的profile,可根据不同的环境:dev,sit等配置不同的profile,配置完成之后将会从激活的profile中上传下载jar包 -->
<profiles>
<profile>
<id>nexusProfile</id>
<repositories>
<repository>
<id>nexusId</id>
<name>Repository for nexus</name>
<url>http://localhost:8081/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
<updatePolicy>never</updatePolicy>
<checksumPolicy>warn</checksumPolicy>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
</profile>
</profiles>
<!-- 激活profile -->
<activeProfiles>
<activeProfile>nexusProfile</activeProfile>
</activeProfiles>
7,配置pom.xml,贴一个父子POM的常用依赖
<!-- 父类POM,主要是管理jar包版本,管理子模块,设置整个模块的JDK版本,打包依赖版本,私服上传设置 -->
<?xml version="1.0" encoding="UTF-8"?>
<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.tate</groupId>
<artifactId>code-all</artifactId>
<version>0.0.3-SNAPSHOT</version>
<packaging>pom</packaging>
<description>tate的代码集合</description>
<!-- 配置配置 -->
<properties>
<project.version>0.0.3-SNAPSHOT</project.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<dependency.junit.version>4.9</dependency.junit.version>
<dependency.spring.version>4.1.6.RELEASE</dependency.spring.version>
</properties>
<!-- 导入模块 -->
<modules>
<!-- 聚合是模块的位置 -->
<module>code-helloword-test</module>
<module>code-spring-restful</module>
<module>code-model</module>
<module>code-monitor</module>
<module>code-dubbo</module>
<module>code-core</module>
<module>code-core-web</module>
<module>code-model-module</module>
</modules>
<!-- Management:可让子工程集成,此处定义包的version和scope,子项目引用即可 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${dependency.junit.version}</version>
<scope>compile</scope>
</dependency>
<!-- spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${dependency.spring.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<!-- mvn:clear deploy配置私有仓库定义 -->
<distributionManagement>
<snapshotRepository>
<id>helloword-snapshots</id>
<name>User project SNAPSHOTS</name>
<url>http://localhost:8081/nexus/content/repositories/snapshots/</url>
</snapshotRepository>
<repository>
<id>helloword-release</id>
<name>User project RELEASE</name>
<url>http://localhost:8081/nexus/content/repositories/releases/</url>
</repository>
</distributionManagement>
<!-- 连接的仓库地址,此处连接的是自己的私服地址 -->
<repositories>
<repository>
<id>nexusId</id>
<name>Repository for nexus</name>
<url>http://localhost:8081/nexus/content/groups/public/</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<!-- maven build插件使用 ,添加Management供子类使用 -->
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.3.5.v20151012</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<!-- 全局编译插件,解决项目刷新之后JDK默认变成1.5 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<encoding>UTF-8</encoding>
<source>1.8</source>
<target>1.8</target>
<skip>false</skip>
</configuration>
</plugin>
<plugin>
<!-- 插件信息,deploy源码 -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.0.0</version>
<!-- 定义怎么样执行 -->
<executions>
<execution>
<!-- 绑定在哪个生命周期 -->
<phase>generate-sources</phase>
<!-- 绑定在命令的哪个参数 -->
<goals>
<goal>jar</goal>
<goal>test-jar</goal>
</goals>
</execution>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
<!-- 子类POM,引入需要的依赖,配置具体的打包运行信息 -->
<?xml version="1.0" encoding="UTF-8"?>
<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.tate</groupId>
<artifactId>code-all</artifactId>
<version>0.0.3-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>code-spring-restful</artifactId>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>com.tate</groupId>
<artifactId>code-core</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<!-- 全局编译插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<configuration>
<webApp>
<contextPath>/spring-restful</contextPath>
</webApp>
</configuration>
</plugin>
</plugins>
</build>
</project>
一、 备注
1,运行参数:
clean compile: 只编译
clean test:执行测试
clean package:打包
clean install:上传到本地资源库
clean deploy:上传到maven资源服务器
详细的执行计划:
validate
generate-sources
process-sources
generate-resources
process-resources 复制并处理资源文件,至目标目录,准备打包。
compile 编译项目的源代码。
process-classes
generate-test-sources
process-test-sources
generate-test-resources
process-test-resources 复制并处理资源文件,至目标测试目录。
test-compile 编译测试源代码。
process-test-classes
test 使用合适的单元测试框架运行测试。这些测试代码不会被打包或部署。
prepare-package
package 接受编译好的代码,打包成可发布的格式,如 JAR 。
pre-integration-test
integration-test
post-integration-test
verify
install 将包安装至本地仓库,以让其它项目依赖。
deploy 将最终的包复制到远程的仓库,以让其它开发人员与项目共享。
2,项目用maven管理一般是按照分模块或者分层来建立,分小的块来构建
3,maven中利用spring-test包进行web项目的测试
4,上传MAVEN不受管理的公共jar包到私服,如oracle驱动包:
安装:
mvn install:install-file -DgroupId=com.xy.oracle -DartifactId=ojdbc14 -Dversion=10.2.0.4.0 -Dpackaging=jar -Dfile=E:\ojdbc14.jar
部署:
mvn deploy:deploy-file -DgroupId=com.xy.oracle -DartifactId=ojdbc14 -Dversion=10.2.0.4.0 -Dpackaging=jar -Dfile=E:\ojdbc14.jar -Durl=http://localhost:8081/nexus/content/repositories/thirdparty/ -DrepositoryId=helloword-snapshots
一、 注意事项
1,如清除或者修改了一些maven的依赖,发现eclipse的视图页面并没有刷新,请先project--clean一下代码,在install一下最上层父类的pom文件。
2,如mvn命令报错了,可以在mvn 命令后面加上-X查看详细信息,如:clean -Djetty.port=9091 jetty:run -X
3,maven有一个很好的学习视频,花一两天时间好好看看,基本上就能理解一大半(至少在工作中应该就是同事跟你请假maven相关问题了),丢失了打包下载的地址,自己根据下面的文件名百度一下就可以找到了:
4,之前提到的问题一:更改目录结构:
项目结构
--core
|--java(公共类)
|--resources(公共配置文件,集成定时器其他公共包)
--功能A
|--java
|--功能A实体BEAN
|--功能A数据操作
|--功能A业务
|--功能A请求
|--resources
|--配置文件A
--功能B
|--java
|--功能B实体BEAN
|--功能B数据操作
|--功能B业务
|--功能B请求
|--resources
|--配置文件B
--功能C
|--java
|--功能C实体BEAN
|--功能C数据操作
|--功能C业务
|--功能C请求
|--resources
|--配置文件C
--页面(前后端分离)
|--webapps
|--页面A
|--页面B
|--页面C
5
,断点进不了源码问题:在pom点击邮件,RUN AS--resources添加源码项目即可