项目改成了maven管理,现场需要用增量补丁包的形式发布代码;
2015/4/21
以前试过用ant打补丁包,现在试试maven能不能做同样的事情;
maven-assembly-plugin看着可以做这样的事情;http://www.infoq.com/cn/news/2011/06/xxb-maven-9-package/
pom.xml配置:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>2.2.1</version> <configuration> <descriptors> <descriptor>src/main/assembly/assembly.xml</descriptor> </descriptors> </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin>
assembly.xml:
<assembly> <id>bin</id> <formats> <format>zip</format> </formats> <dependencySets> <dependencySet> <useProjectArtifact>true</useProjectArtifact> <outputDirectory>lib</outputDirectory> </dependencySet> </dependencySets> <fileSets> <fileSet> <outputDirectory>/</outputDirectory> <includes> <include>README.txt</include> </includes> </fileSet> <!-- <fileSet> <directory>src/main/scripts</directory> <outputDirectory>/bin</outputDirectory> <includes> <include>run.sh</include> <include>run.bat</include> </includes> </fileSet> --> </fileSets> </assembly>
运行maven clean package;
第一次运行要下载很多包;
看看srpm-bin.zip都有什么;
lib里边是所有依赖jar和war包;
现在要的是只打包部分class和配置,不需要打包依赖和不生成war包;
看看怎么配置:http://maven.apache.org/plugins/maven-assembly-plugin/assembly.html
/controllers/HomeController*.class
配置加了通配符*,没生效;
[INFO] Reading assembly descriptor: src/main/assembly/assembly.xml
[WARNING] The following patterns were never triggered in this artifact inclusion filter:
哈哈,这里只是用fileSet就可以了;
增加一个试试:
<fileSet>
<outputDirectory>/WEB-INF/classes</outputDirectory>
<includes>
<include>target/classes/com/xxx/srpm/controllers/HomeController.class</include>
</includes>
</fileSet>
打包结果路径是:
WEB-INF\classes\target\classes\com\xxx\srpm\controllers
需要的是:
WEB-INF\classes\com\xxx\srpm\controllers
因为这样和war包的目录结构一样,升级的时候直接拖动文件夹覆盖war包里边的就可以了;
看文档貌似directory能设置基准路径;
<fileSet>
<outputDirectory>/WEB-INF/classes</outputDirectory>
<directory>target/classes/</directory>
<includes>
<include>com/xxx/srpm/controllers/HomeController.class</include>
</includes>
</fileSet>
完美!就是这样配置了;
完整的配置如下:
<assembly> <id>upd-20150421-1</id> <includeBaseDirectory>false</includeBaseDirectory> <formats> <format>zip</format> </formats> <fileSets> <fileSet> <outputDirectory>/WEB-INF/classes</outputDirectory> <directory>target/classes/</directory> <includes> <include>com/xxx/srpm/controllers/HomeController.class</include> <include>com/xxx/srpm/activiti/DetailBuilder.class</include> <include>com/xxx/srpm/activiti/common/HistoricProcessInstanceSQLFragment.class</include> <include>com/xxx/srpm/activiti/query/HistoricProcessInstanceQuery.class</include> <include>com/xxx/srpm/base/dao/BaseDao.class</include> <include>com/xxx/srpm/base/dao/SQL.class</include> <include>com/xxx/srpm/base/query/BaseQuery.class</include> <include>com/xxx/srpm/base/util/BaseUtils.class</include> <include>com/xxx/srpm/base/util/PageUtils.class</include> <include>com/xxx/srpm/repository/activiti/HistoricProcessInstanceRepository.class</include> </includes> </fileSet> <fileSet> <outputDirectory>/</outputDirectory> <includes> <include>README.txt</include> </includes> </fileSet> </fileSets> </assembly>
再写个Helper:
/** * assembly.xml配置工具类 * @author yejq */ public class AssemblyHelper { private final static String CLASS_PATH; static { File file = new File(""); CLASS_PATH = file.getAbsolutePath(); } /** * 将java的路径转换为class路径 */ @Test public void java2class(){ String path = CLASS_PATH + "/src/test/java/build/java.txt"; BufferedReader reader = null; try { reader = new BufferedReader(new FileReader(path)); String line = null; StringBuilder builder = new StringBuilder(); while ((line = reader.readLine()) != null) { if (StringUtils.isNotBlank(line)) { line = line.trim().replace("/srpm/src/main/java/", " <include>") .replace(".java", ".class</include>"); builder.append(line).append(BaseUtils.lineSeparator); } } System.out.println(builder.toString()); } catch (Exception e) { e.printStackTrace(); }finally{ if (reader != null) { try { reader.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
maven-assembly-plugin挺好用,好强大!
再加上个通配符,避免遗漏内部类;
<include>com/xxx/srpm/controllers/HomeController*.class</include>
总结:
1、解决方案很多,用哪个,究竟哪个更适合更优雅,用过才知道,或许不是最佳实践,但没试过,怎么知道是不是最佳呢?这就是架构做的事情:在解决方案之间做决策,并提供最佳实践,指导开发人员实践;
2、工作了一周,问同事有没有签合同,了解到可能是转正签,之前遇到的都是入职签,这转正签会不会有问题?