maven打包后pom.properties中的注释问题

探讨Maven打包过程中生成的pom.properties文件内时间戳导致的问题,及如何通过代码改进来解决此问题,确保同一源码生成相同包。

使用maven打包后,在META-INF目录下会生成pom.properties文件(当然可以使其不生成)。这个文件包含了包的Id信息,此外它最开始有两行注释,看起来是这样的

#Generated by Maven
#Sat Jun 25 09:40:37 CST 2016

 第一行 是固定的,第二行是打包时候的时间戳。

第二行的存在有一个严重的问题,就是我们完全不修改代码,然后两次打包由于时间戳不一样,导致生成的两个包不一样。如果你不在乎可能觉得没有什么,但是对于大型项目,代码没变包却不同导致不能进行增量部署。

这个代码的出现是由于java.util.Properties类的store(Writer writer, String comments)方法中有一行

bw.write("#" + new Date().toString());

 这个问题在困扰大家的同时,也困扰着Maven的开发者。与之相关的有两个issue:

MSHARED-494

MSHARED-505

maven人员郁闷的说

Oracle's implementation of store() does write the stupid new Date().toString()

 由于大家需求强烈,目前该特性据说已被修正。

在今年5月21号提交的代码中,时间这一行注释被移除了:移除方法是把生成后的文件对行迭代,看到是注释就删除。

Stupid hack: write the properties to a StringWriter, 
iterate with a BufferedReader and drop all comments, 
finall write real content to the target file.

 我们看一下中心库中的版本和时间:



 3.0.x版本最晚是4月份提交的,所以它不包含这个改动。3.1.x最早是6月份提交的,现在已经有两个小版本了,但是引用次数还是0.

我们对比一下3.0和3.1中的代码。首先是3.0.2中的:

75 	    private void createPropertiesFile( MavenSession session, Properties properties, File outputFile,
76 	                                       boolean forceCreation )
77 	        throws IOException
78 	    {
79 	        File outputDir = outputFile.getParentFile();
80 	        if ( outputDir != null && !outputDir.isDirectory() && !outputDir.mkdirs() )
81 	        {
82 	            throw new IOException( "Failed to create directory: " + outputDir );
83 	        }
84 	        if ( !forceCreation && sameContents( properties, outputFile ) )
85 	        {
86 	            return;
87 	        }
88 	        OutputStream os = new FileOutputStream( outputFile );
89 	        try
90 	        {
91 	            String createdBy = CREATED_BY_MAVEN;
92 	            if ( session != null ) // can be null due to API backwards compatibility
93 	            {
94 	                String mavenVersion = session.getSystemProperties().getProperty( "maven.version" );
95 	                if ( mavenVersion != null )
96 	                {
97 	                    createdBy += " " + mavenVersion;
98 	                }
99 	            }
100 	
101 	            properties.store( os, createdBy );
102 	            os.close(); // stream is flushed but not closed by Properties.store()
103 	            os = null;
104 	        }
105 	        finally
106 	        {
107 	            IOUtil.close( os );
108 	        }
109 	    }

 

 下面是3.1.1的(左边是在文件内的行号):

77          private void createPropertiesFile( MavenSession session, Properties properties, File outputFile,
78 	                                       boolean forceCreation )
79 	        throws IOException
80 	    {
81 	        File outputDir = outputFile.getParentFile();
82 	        if ( outputDir != null && !outputDir.isDirectory() && !outputDir.mkdirs() )
83 	        {
84 	            throw new IOException( "Failed to create directory: " + outputDir );
85 	        }
86 	        if ( !forceCreation && sameContents( properties, outputFile ) )
87 	        {
88 	            return;
89 	        }
90 	        PrintWriter pw = new PrintWriter( outputFile, "ISO-8859-1" );
91 	        try
92 	        {
93 	            String createdBy = CREATED_BY_MAVEN;
94 	            if ( session != null ) // can be null due to API backwards compatibility
95 	            {
96 	                String mavenVersion = session.getSystemProperties().getProperty( "maven.version" );
97 	                if ( mavenVersion != null )
98 	                {
99 	                    createdBy += " " + mavenVersion;
100 	                }
101 	            }
102 	
103 	            StringWriter sw = new StringWriter();
104 	            properties.store( sw, null );
105 	
106 	            BufferedReader r = new BufferedReader( new StringReader( sw.toString() ) );
107 	
108 	            pw.println( "#" + createdBy );
109 	            String line;
110 	            while ( ( line = r.readLine() ) != null )
111 	            {
112 	                if ( !line.startsWith( "#" ) )
113 	                {
114 	                    pw.println( line );
115 	                }
116 	            }
117 	
118 	            r.close();
119 	            r = null;
120 	            sw.close();
121 	            sw = null;
122 	            pw.close();
123 	            pw = null;
124 	        }
125 	        finally
126 	        {
127 	            IOUtil.close( pw );
128 	        }
129 	    }

 这里主要是对注释的处理,正文内容的处理在方法public void createPomProperties()中。

下面版本比上面多了一个流程,就是临时变量sw写入后,再逐行读出来,不是注释就写入pw中(迭代前已经把maven信息写好了)。这样就把时间删掉了。

 

 

@ECHO OFF echo ^<?xml version="1.0" encoding="UTF-8"?^> > pom.xml echo ^<project xmlns="http://maven.apache.org/POM/4.0.0" >> pom.xml echo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >> pom.xml echo xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"^> >> pom.xml echo ^<modelVersion^>4.0.0^</modelVersion^> >> pom.xml echo ^<packaging^>pom^</packaging^> >> pom.xml echo ^<groupId^>com.tplink.smb.omada^</groupId^> >> pom.xml echo ^<artifactId^>omada^</artifactId^> >> pom.xml echo ^<version^>5.0.0^</version^> >> pom.xml echo ^<properties^> >> pom.xml echo ^<java.version^>1.8^</java.version^> >> pom.xml echo ^</properties^> >> pom.xml echo ^<modules^> >> pom.xml echo ^<module^>omada_sdn_controller_microservice^</module^>" >> pom.xml echo ^<module^>omada_web^</module^> >> pom.xml echo ^<module^>omada_api_gateway^</module^> >> pom.xml echo ^<module^>omada_client^</module^> >> pom.xml echo ^<module^>omada_components^</module^> >> pom.xml echo ^<module^>omada_device_gateway^</module^> >> pom.xml echo ^<module^>omada_identityaccess^</module^> >> pom.xml echo ^<module^>omada_log^</module^> >> pom.xml echo ^<module^>omada_manager^</module^> >> pom.xml echo ^<module^>omada_monitor^</module^> >> pom.xml echo ^<module^>omada_maintenance^</module^> >> pom.xml echo ^<module^>omada_starter^</module^> >> pom.xml echo ^<module^>omada_insight^</module^> >> pom.xml echo ^</modules^> >> pom.xml echo ^</project^> >> pom.xml SET JAVA_HOME=%java_path% echo %JAVA_HOME% "%JAVA_HOME%\bin\java.exe" -version SET PATH=%JAVA_HOME%/bin;%maven_path%;%PATH% echo %PATH% call mvn -v REM 运行sonarqube分析 REM %maven_path%mvn.cmd clean verify -Plocal-build -Prelease-win -Psmb-nexus -Dmaven.test.skip=true org.sonarsource.scanner.maven:sonar-maven-plugin:3.11.0.3922:sonar -Dsonar.scm.disabled=true -#Dsonar.projectKey=win_%version% -Dsonar.projectName=win_%version% -Dsonar.host.url=http://10.42.20.74 -Dsonar.token=2c0b2622199955495c5e478acac658353bbcaa14 -pl !omada_web REM 强制刷新parent REM cd omada_sdn_controller_microservice REM %maven_path%mvn.cmd install REM cd .. echo Skip_Web=%Skip_Web% IF "%Skip_Web%"=="true" ( %maven_path%mvn.cmd clean install -Plocal-build -Prelease-win -Poversea -Psmb-nexus -Dmaven.test.skip=true -U -T1C -pl !omada_web ) else ( %maven_path%mvn.cmd clean install -Plocal-build -Prelease-win -Poversea -Psmb-nexus -Dmaven.test.skip=true -U -T1C ) REM 拷贝到深圳-美国中转目录中,加速Windows安装包服务器下载 SET PACKAGE_PATH=Z:\windows_packages\omada-standard\%BUILD_NUMBER% MKDIR %PACKAGE_PATH% COPY omada_starter\local-starter\target\*.zip %PACKAGE_PATH% 这段代码什么意思
最新发布
10-28
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值