分享

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

 liang1234_ 2019-05-19

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

Xml代码 收藏代码
  1. #Generated by Maven  

  2. #Sat Jun 25 09:40:37 CST 2016  

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

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

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

Java代码 收藏代码
  1. bw.write("#" + new Date().toString());  

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

MSHARED-494

MSHARED-505

maven人员郁闷的说

Xml代码 收藏代码
  1. Oracle's implementation of store() does write the stupid new Date().toString()  

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

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

Xml代码 收藏代码
  1. Stupid hack: write the properties to a StringWriter,   

  2. iterate with a BufferedReader and drop all comments,   

  3. finall write real content to the target file.  

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



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

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

Java代码 收藏代码
  1. 75      private void createPropertiesFile( MavenSession session, Properties properties, File outputFile,  

  2. 76                                         boolean forceCreation )  

  3. 77          throws IOException  

  4. 78      {  

  5. 79          File outputDir = outputFile.getParentFile();  

  6. 80          if ( outputDir != null && !outputDir.isDirectory() && !outputDir.mkdirs() )  

  7. 81          {  

  8. 82              throw new IOException( "Failed to create directory: " + outputDir );  

  9. 83          }  

  10. 84          if ( !forceCreation && sameContents( properties, outputFile ) )  

  11. 85          {  

  12. 86              return;  

  13. 87          }  

  14. 88          OutputStream os = new FileOutputStream( outputFile );  

  15. 89          try  

  16. 90          {  

  17. 91              String createdBy = CREATED_BY_MAVEN;  

  18. 92              if ( session != null ) // can be null due to API backwards compatibility  

  19. 93              {  

  20. 94                  String mavenVersion = session.getSystemProperties().getProperty( "maven.version" );  

  21. 95                  if ( mavenVersion != null )  

  22. 96                  {  

  23. 97                      createdBy += " " + mavenVersion;  

  24. 98                  }  

  25. 99              }  

  26. 100       

  27. 101                 properties.store( os, createdBy );  

  28. 102                 os.close(); // stream is flushed but not closed by Properties.store()  

  29. 103                 os = null;  

  30. 104             }  

  31. 105             finally  

  32. 106             {  

  33. 107                 IOUtil.close( os );  

  34. 108             }  

  35. 109         }  

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

Java代码 收藏代码
  1. 77          private void createPropertiesFile( MavenSession session, Properties properties, File outputFile,  

  2. 78                                         boolean forceCreation )  

  3. 79          throws IOException  

  4. 80      {  

  5. 81          File outputDir = outputFile.getParentFile();  

  6. 82          if ( outputDir != null && !outputDir.isDirectory() && !outputDir.mkdirs() )  

  7. 83          {  

  8. 84              throw new IOException( "Failed to create directory: " + outputDir );  

  9. 85          }  

  10. 86          if ( !forceCreation && sameContents( properties, outputFile ) )  

  11. 87          {  

  12. 88              return;  

  13. 89          }  

  14. 90          PrintWriter pw = new PrintWriter( outputFile, "ISO-8859-1" );  

  15. 91          try  

  16. 92          {  

  17. 93              String createdBy = CREATED_BY_MAVEN;  

  18. 94              if ( session != null ) // can be null due to API backwards compatibility  

  19. 95              {  

  20. 96                  String mavenVersion = session.getSystemProperties().getProperty( "maven.version" );  

  21. 97                  if ( mavenVersion != null )  

  22. 98                  {  

  23. 99                      createdBy += " " + mavenVersion;  

  24. 100                     }  

  25. 101                 }  

  26. 102       

  27. 103                 StringWriter sw = new StringWriter();  

  28. 104                 properties.store( sw, null );  

  29. 105       

  30. 106                 BufferedReader r = new BufferedReader( new StringReader( sw.toString() ) );  

  31. 107       

  32. 108                 pw.println( "#" + createdBy );  

  33. 109                 String line;  

  34. 110                 while ( ( line = r.readLine() ) != null )  

  35. 111                 {  

  36. 112                     if ( !line.startsWith( "#" ) )  

  37. 113                     {  

  38. 114                         pw.println( line );  

  39. 115                     }  

  40. 116                 }  

  41. 117       

  42. 118                 r.close();  

  43. 119                 r = null;  

  44. 120                 sw.close();  

  45. 121                 sw = null;  

  46. 122                 pw.close();  

  47. 123                 pw = null;  

  48. 124             }  

  49. 125             finally  

  50. 126             {  

  51. 127                 IOUtil.close( pw );  

  52. 128             }  

  53. 129         }  

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

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

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多