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

小编 2026-06-09 阅读:1372 评论:0
使用maven打包后,在META-INF目录下会生成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.

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


maven打包后pom.properties中的注释问题
 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 IOException78 	    {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 	        try90 	        {91 	            String createdBy = CREATED_BY_MAVEN;92 	            if ( session != null ) // can be null due to API backwards compatibility93 	            {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 	        finally106 	        {107 	            IOUtil.close( os );108 	        }109 	    }

 

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

77          private void createPropertiesFile( MavenSession session, Properties properties, File outputFile,78 	                                       boolean forceCreation )79 	        throws IOException80 	    {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 	        try92 	        {93 	            String createdBy = CREATED_BY_MAVEN;94 	            if ( session != null ) // can be null due to API backwards compatibility95 	            {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 	        finally126 	        {127 	            IOUtil.close( pw );128 	        }129 	    }

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

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

 

 

版权声明

本文仅代表作者观点,不代表百度立场。
本文系作者授权百度百家发表,未经许可,不得转载。

热门文章
  • 机房智能化温湿度解决方式之POE供电以太网温湿度传感器

    机房智能化温湿度解决方式之POE供电以太网温湿度传感器
    机房智能化温湿度解决方式之POE供电以太网温湿度传感器 北京盈创力和电子科技有限公司 智能型TCP网口温湿度记录仪 北京IP网络温湿度记录仪厂家,北京盈创力和 北京智能型TCP网口温湿度记录仪IP网络温湿度记录仪是一种新型的基于TCP/IP协议双绞线以太网标准温湿度采集模块,利用它可以实现现场温度值、相对湿度值的采集,同时利用其自身的RJ45通信接口可以方便地和机房监控主机或交换机集线器进行联网。 工作于-40℃~85℃工业级带...
  • Sequential Monte Carlo Methods (SMC) 序列蒙特卡洛/粒子滤波/Bootstrap Filtering

    Sequential Monte Carlo Methods (SMC) 序列蒙特卡洛/粒子滤波/Bootstrap Filtering
    Problem Statement 我们考虑一个具有马尔可夫性质、非线性、非高斯的状态空间模型(State Space Model):对于一个时间序列上的观测结果{yt,t∈N}\\{ y_t , t \\in N \\}{yt​,t∈N},我们认为每个观测结果yty_tyt​的生成依赖于一个无法直接观察的隐变量xt∈{xt,t∈N}x_t \\in \\{x_t , t \\in N \\}xt​∈{xt​,t∈N},即:p(...
  • HTTP状态保持的原理

    HTTP状态保持的原理
    a)在用户登录之后,浏览器返回响应的时候会在响应中添加上cookieb)浏览器接收到cookie之后会自动保存c)当用户再次请求同一服务器中的其他网页的时候,浏览器会自动带上之前保存的cookied)服务接收到请求之后可以请 request 对象中取到cookie 判断当前用户是否登录  Http是无状态的,就是连接时数据互通,关闭后...
  • Hive 系统函数及示例

    Hive 系统函数及示例
    查看所有系统函数 show functions; 函数分类 内置函数【系统函数】 数学函数: floor、round、ceil、cos、log2等 字符串函数: length、reverse、trim、lower、get_json_object、repeat等 收集函数: size 转换函数: cast 日期函数: year、month、datediff、date、date_add等 条件函数: coalesce、case…w...
  • CSRF的原理和防范措施

    CSRF的原理和防范措施
    a)攻击原理:i.用户C访问正常网站A时进行登录,浏览器保存A的cookieii.用户C再访问攻击网站B,网站B上有某个隐藏的链接或者图片标签会自动请求网站A的URL地址,例如表单提交,传指定的参数iii.而攻击网站B在访问网站A的时候,浏览器会自动带上网站A的cookieiv.所以网站A在接收到请求之后可判断当前用户是登录状态,所以...
标签列表