最近项目中,有个主干版本,然后来了个新需求,在原有需求中升级,前提是原有需求还得继续使用,按以前的习惯方式是拉取分支代码版本作管理,然后做代码合并。这次考虑分支与主干版本的周期跨越太大,于是用到了maven的包继承,版本稳定后,去除旧的包。
1.jar包继承
现有两个模块:
com.isoftstone.hyom.pickingcode 这是原有模块包.
com.isoftstone.hyom.pickingcode 这是新加的模块包,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.isoftstone.hyom</groupId>
<artifactId>com.isoftstone.hyom</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>com.isoftstone.hyom.pickingcode_new</artifactId>
<name>com.isoftstone.hyom.pickingcode_new</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>
<dependency>
<groupId>com.isoftstone.hyom</groupId>
<artifactId>com.isoftstone.hyom.pickingcode</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<artifactSet>
<includes>
<include>com.isoftstone.hyom:com.isoftstone.hyom.pickingcode</include>
</includes>
<excludes>
<exclude>*:*:jar</exclude>
</excludes>
</artifactSet>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</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.isoftstone.hyom</groupId>
<artifactId>com.isoftstone.hyom</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>com.isoftstone.hyom.pickingcode_new</artifactId>
<name>com.isoftstone.hyom.pickingcode_new</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>
<dependency>
<groupId>com.isoftstone.hyom</groupId>
<artifactId>com.isoftstone.hyom.pickingcode</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<artifactSet>
<includes>
<include>com.isoftstone.hyom:com.isoftstone.hyom.pickingcode</include>
</includes>
<excludes>
<exclude>*:*:jar</exclude>
</excludes>
</artifactSet>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
2.war包继承
同理两个war包
uomp-lec 原有war包
uomp-lec-new 新建war包,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>
<parent>
<groupId>com.isoftstone.hyom</groupId>
<artifactId>com.isoftstone.hyom.build.war</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>uomp-lec-new</artifactId>
<packaging>war</packaging>
<properties>
<endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
</properties>
<dependencies>
<!-- 引入原有war包 -->
<dependency>
<groupId>com.isoftstone.hyom</groupId>
<artifactId>uomp-lec</artifactId>
<version>0.0.1-SNAPSHOT</version>
<type>war</type>
</dependency>
<!-- 引入新的jar包 -->
<dependency>
<groupId>com.isoftstone.hyom</groupId>
<artifactId>com.isoftstone.hyom.outeradmin_new</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.isoftstone.hyom</groupId>
<artifactId>com.isoftstone.hyom.pickingcode_new</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<compilerArguments>
<endorseddirs>${endorsed.dir}</endorseddirs>
</compilerArguments>
</configuration>
</plugin>
<!-- 排除原有jar包 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
<packagingExcludes>WEB-INF/lib/com.isoftstone.hyom.outeradmin-0.0.1-SNAPSHOT.jar,WEB-INF/lib/com.isoftstone.hyom.pickingcode-0.0.1-SNAPSHOT.jar</packagingExcludes>
</configuration>
<executions>
</executions>
</plugin>
<!-- 排除原有jar包 -->
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.2.3.v20140905</version>
<configuration>
<packagingExcludes>WEB-INF/lib/com.isoftstone.hyom.outeradmin-0.0.1-SNAPSHOT.jar,WEB-INF/lib/com.isoftstone.hyom.pickingcode-0.0.1-SNAPSHOT.jar</packagingExcludes>
<scanIntervalSeconds>10</scanIntervalSeconds>
<reload>manual</reload>
<webApp>
<contextPath>/uomp-lec</contextPath>
</webApp>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
</plugin>
</plugins>
<finalName>uomp-lec</finalName>
</build>
</project>
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>
<parent>
<groupId>com.isoftstone.hyom</groupId>
<artifactId>com.isoftstone.hyom.build.war</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>uomp-lec-new</artifactId>
<packaging>war</packaging>
<properties>
<endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
</properties>
<dependencies>
<!-- 引入原有war包 -->
<dependency>
<groupId>com.isoftstone.hyom</groupId>
<artifactId>uomp-lec</artifactId>
<version>0.0.1-SNAPSHOT</version>
<type>war</type>
</dependency>
<!-- 引入新的jar包 -->
<dependency>
<groupId>com.isoftstone.hyom</groupId>
<artifactId>com.isoftstone.hyom.outeradmin_new</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.isoftstone.hyom</groupId>
<artifactId>com.isoftstone.hyom.pickingcode_new</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<compilerArguments>
<endorseddirs>${endorsed.dir}</endorseddirs>
</compilerArguments>
</configuration>
</plugin>
<!-- 排除原有jar包 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
<packagingExcludes>WEB-INF/lib/com.isoftstone.hyom.outeradmin-0.0.1-SNAPSHOT.jar,WEB-INF/lib/com.isoftstone.hyom.pickingcode-0.0.1-SNAPSHOT.jar</packagingExcludes>
</configuration>
<executions>
</executions>
</plugin>
<!-- 排除原有jar包 -->
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.2.3.v20140905</version>
<configuration>
<packagingExcludes>WEB-INF/lib/com.isoftstone.hyom.outeradmin-0.0.1-SNAPSHOT.jar,WEB-INF/lib/com.isoftstone.hyom.pickingcode-0.0.1-SNAPSHOT.jar</packagingExcludes>
<scanIntervalSeconds>10</scanIntervalSeconds>
<reload>manual</reload>
<webApp>
<contextPath>/uomp-lec</contextPath>
</webApp>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
</plugin>
</plugins>
<finalName>uomp-lec</finalName>
</build>
</project>
3. 在新的模块包中,新建同包名况且同类名时,打包时,可以原有类。
上述源代码打包后,在新包中可以看到,继承了原本所有类,并且覆盖了,同包同类文件,如图:
于是我们就可以在这个新包里面进行开发,如果原本中哪个类需要变动,就在新包中建立同包,同类进行覆盖。