vaimr

http://jira.codehaus.org/browse/MSHARED-154 bug fix

Oct 2nd, 2013
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 14.06 KB | None | 0 0
  1. Index: src/main/java/org/apache/maven/archiver/MavenArchiveConfiguration.java
  2. ===================================================================
  3. --- src/main/java/org/apache/maven/archiver/MavenArchiveConfiguration.java  (revision 1523972)
  4. +++ src/main/java/org/apache/maven/archiver/MavenArchiveConfiguration.java  (working copy)
  5. @@ -59,8 +59,9 @@
  6.       * @since 2.3
  7.       */
  8.      private File pomPropertiesFile;
  9. +    private String pomPropertiesTemplate;
  10.  
  11. -    public boolean isCompress()
  12. +  public boolean isCompress()
  13.      {
  14.          return compress;
  15.      }
  16. @@ -239,4 +240,14 @@
  17.      {
  18.          this.pomPropertiesFile = pomPropertiesFile;
  19.      }
  20. +
  21. +    public void setPomPropertiesTemplate(String template)
  22. +    {
  23. +      pomPropertiesTemplate = template;
  24. +    }
  25. +
  26. +    public String getPomPropertiesTemplate()
  27. +    {
  28. +      return pomPropertiesTemplate;
  29. +    }
  30.  }
  31. Index: src/main/java/org/apache/maven/archiver/MavenArchiver.java
  32. ===================================================================
  33. --- src/main/java/org/apache/maven/archiver/MavenArchiver.java  (revision 1523972)
  34. +++ src/main/java/org/apache/maven/archiver/MavenArchiver.java  (working copy)
  35. @@ -536,14 +536,16 @@
  36.              // ----------------------------------------------------------------------
  37.              // Create pom.properties file
  38.              // ----------------------------------------------------------------------
  39. -
  40. -            File pomPropertiesFile = archiveConfiguration.getPomPropertiesFile();
  41. -            if ( pomPropertiesFile == null )
  42. +            if (StringUtils.isNotEmpty(archiveConfiguration.getPomPropertiesTemplate()))
  43.              {
  44. -                File dir = new File( workingProject.getBuild().getDirectory(), "maven-archiver" );
  45. -                pomPropertiesFile = new File( dir, "pom.properties" );
  46. +               new PomPropertiesUtil().createPomProperties( workingProject, archiver,
  47. +                   archiveConfiguration.getPomPropertiesTemplate(), forced );
  48.              }
  49. -            new PomPropertiesUtil().createPomProperties( workingProject, archiver, pomPropertiesFile, forced );
  50. +            else
  51. +            {
  52. +               File pomPropertiesTemplateFile = archiveConfiguration.getPomPropertiesFile();
  53. +               new PomPropertiesUtil().createPomProperties( workingProject, archiver, pomPropertiesTemplateFile, forced );
  54. +            }
  55.          }
  56.  
  57.          // ----------------------------------------------------------------------
  58. Index: src/main/java/org/apache/maven/archiver/PomPropertiesUtil.java
  59. ===================================================================
  60. --- src/main/java/org/apache/maven/archiver/PomPropertiesUtil.java  (revision 1523972)
  61. +++ src/main/java/org/apache/maven/archiver/PomPropertiesUtil.java  (working copy)
  62. @@ -19,19 +19,15 @@
  63.   * under the License.
  64.   */
  65.  
  66. -import java.io.File;
  67. -import java.io.FileInputStream;
  68. -import java.io.FileOutputStream;
  69. -import java.io.IOException;
  70. -import java.io.InputStream;
  71. -import java.io.OutputStream;
  72. -import java.util.Properties;
  73. -
  74.  import org.apache.maven.project.MavenProject;
  75. +import org.apache.maven.shared.utils.StringUtils;
  76. +import org.apache.maven.shared.utils.io.IOUtil;
  77.  import org.codehaus.plexus.archiver.Archiver;
  78.  import org.codehaus.plexus.archiver.ArchiverException;
  79. -import org.apache.maven.shared.utils.io.IOUtil;
  80.  
  81. +import java.io.*;
  82. +import java.util.Map;
  83. +import java.util.Properties;
  84.  
  85.  /**
  86.   * This class is responsible for creating the pom.properties
  87. @@ -98,23 +94,129 @@
  88.      /**
  89.       * Creates the pom.properties file.
  90.       */
  91. -    public void createPomProperties( MavenProject project, Archiver archiver, File pomPropertiesFile,
  92. +    public void createPomProperties( MavenProject project, Archiver archiver, File pomPropertiesTemplateFile,
  93.                                       boolean forceCreation )
  94.          throws ArchiverException, IOException
  95.      {
  96. +        String template = null;
  97. +        if (pomPropertiesTemplateFile != null && pomPropertiesTemplateFile.exists())
  98. +        {
  99. +            InputStream templateFileStream = new FileInputStream(pomPropertiesTemplateFile);
  100. +            try
  101. +            {
  102. +              template = IOUtil.toString(templateFileStream);
  103. +            }
  104. +            finally
  105. +            {
  106. +              IOUtil.close(templateFileStream);
  107. +            }
  108. +        }
  109. +
  110. +        createPomProperties(project, archiver, template, forceCreation);
  111. +    }
  112. +
  113. +    /**
  114. +     * Creates the pom.properties file.
  115. +     */
  116. +    public void createPomProperties( MavenProject project, Archiver archiver, String template,
  117. +                                   boolean forceCreation )
  118. +      throws ArchiverException, IOException
  119. +    {
  120.          final String artifactId = project.getArtifactId();
  121.          final String groupId = project.getGroupId();
  122.  
  123. -        Properties p = new Properties();
  124. +        File dir = new File( project.getBuild().getDirectory(), "maven-archiver" );
  125. +        File pomPropertiesResultFile = new File( dir, "pom.properties" );
  126.  
  127. -        p.setProperty( "groupId", project.getGroupId() );
  128. +        Properties p = getPreparedProperties(project, template);
  129. +        createPropertyFile( p, pomPropertiesResultFile, forceCreation );
  130.  
  131. -        p.setProperty( "artifactId", project.getArtifactId() );
  132. +        archiver.addFile( pomPropertiesResultFile, "META-INF/maven/" + groupId + "/" + artifactId + "/pom.properties" );
  133. +    }
  134.  
  135. -        p.setProperty( "version", project.getVersion() );
  136. +  /**
  137. +   * Return properties for make pom.properties by template
  138. +   *
  139. +   * @param project Current project
  140. +   * @param template Properties template file or null
  141. +   * @return Properties
  142. +   * @throws IOException If error read template file
  143. +   */
  144. +    private Properties getPreparedProperties(MavenProject project, String template) throws IOException {
  145. +        Properties properties = new Properties();
  146. +        if (template == null || StringUtils.isEmpty(template))
  147. +        {
  148. +            properties.setProperty( "groupId", project.getGroupId() );
  149. +            properties.setProperty( "artifactId", project.getArtifactId() );
  150. +            properties.setProperty( "version", project.getVersion() );
  151. +            return properties;
  152. +        }
  153.  
  154. -        createPropertyFile( p, pomPropertiesFile, forceCreation );
  155. +        InputStream propertiesTemplateStream = null;
  156. +        try
  157. +        {
  158. +            propertiesTemplateStream = new ByteArrayInputStream(template.getBytes());
  159. +            properties.load(propertiesTemplateStream);
  160.  
  161. -        archiver.addFile( pomPropertiesFile, "META-INF/maven/" + groupId + "/" + artifactId + "/pom.properties" );
  162. +            fillProperties(project, properties);
  163. +
  164. +            return properties;
  165. +        }
  166. +        catch (FileNotFoundException e)
  167. +        {
  168. +            throw new IOException(e.getMessage(), e);
  169. +        }
  170. +        finally
  171. +        {
  172. +            IOUtil.close(propertiesTemplateStream);
  173. +        }
  174.      }
  175. +
  176. +    /**
  177. +     * Fill property values with values from project data
  178. +     *
  179. +     * @param project Current project
  180. +     * @param properties Work properties
  181. +     */
  182. +    private void fillProperties(MavenProject project, Properties properties) {
  183. +        for (Map.Entry<Object, Object> property : properties.entrySet())
  184. +        {
  185. +            if (property.getValue() == null)
  186. +            {
  187. +                continue;
  188. +            }
  189. +
  190. +            String propertyValue = property.getValue().toString();
  191. +            if (propertyValue.startsWith("${"))
  192. +            {
  193. +                String projectProperty = propertyValue.replace("${", "").replace("}", "");
  194. +                if (StringUtils.isEmpty(projectProperty))
  195. +                {
  196. +                    continue;
  197. +                }
  198. +
  199. +                // todo: use velocity engine
  200. +                if (project.getProperties().containsKey(projectProperty))
  201. +                {
  202. +                    property.setValue(project.getProperties().getProperty(projectProperty));
  203. +                }
  204. +                else if ("project.artifactId".equals(projectProperty))
  205. +                {
  206. +                    property.setValue(project.getArtifactId());
  207. +                }
  208. +                else if ("project.groupId".equals(projectProperty))
  209. +                {
  210. +                    property.setValue(project.getGroupId());
  211. +                }
  212. +                else if ("project.version".equals(projectProperty))
  213. +                {
  214. +                    property.setValue(project.getVersion());
  215. +                }
  216. +                else if ("project.description".equals(projectProperty))
  217. +                {
  218. +                    property.setValue(project.getDescription());
  219. +                }
  220. +            }
  221. +        }
  222. +    }
  223.  }
  224. Index: src/test/java/org/apache/maven/archiver/MavenArchiverTest.java
  225. ===================================================================
  226. --- src/test/java/org/apache/maven/archiver/MavenArchiverTest.java  (revision 1523972)
  227. +++ src/test/java/org/apache/maven/archiver/MavenArchiverTest.java  (working copy)
  228. @@ -33,26 +33,18 @@
  229.  import org.apache.maven.monitor.event.EventDispatcher;
  230.  import org.apache.maven.project.MavenProject;
  231.  import org.apache.maven.settings.Settings;
  232. +import org.apache.maven.shared.utils.StringUtils;
  233. +import org.apache.maven.shared.utils.io.FileUtils;
  234.  import org.codehaus.plexus.PlexusContainer;
  235.  import org.codehaus.plexus.archiver.jar.JarArchiver;
  236.  import org.codehaus.plexus.archiver.jar.ManifestException;
  237. -import org.apache.maven.shared.utils.io.FileUtils;
  238. -import org.apache.maven.shared.utils.StringUtils;
  239.  
  240.  import java.io.File;
  241.  import java.io.IOException;
  242. +import java.io.InputStream;
  243.  import java.net.URI;
  244.  import java.net.URL;
  245. -import java.util.ArrayList;
  246. -import java.util.Collections;
  247. -import java.util.Comparator;
  248. -import java.util.Date;
  249. -import java.util.HashMap;
  250. -import java.util.List;
  251. -import java.util.Map;
  252. -import java.util.Properties;
  253. -import java.util.Set;
  254. -import java.util.TreeSet;
  255. +import java.util.*;
  256.  import java.util.jar.Attributes;
  257.  import java.util.jar.JarFile;
  258.  import java.util.jar.Manifest;
  259. @@ -603,6 +595,70 @@
  260.      }
  261.  
  262.      @SuppressWarnings( "ResultOfMethodCallIgnored" )
  263. +    public void testCustomMavenPropertiesFile()
  264. +        throws Exception
  265. +    {
  266. +        MavenSession session = getDummySession();
  267. +        MavenProject project = getDummyProject();
  268. +        File jarFile = new File( "target/test/dummy.jar" );
  269. +        JarArchiver jarArchiver = getCleanJarArciver( jarFile );
  270. +
  271. +        project.getProperties().setProperty("customProperty", "value");
  272. +
  273. +        MavenArchiver archiver = getMavenArchiver( jarArchiver );
  274. +
  275. +        MavenArchiveConfiguration config = new MavenArchiveConfiguration();
  276. +
  277. +        config.setAddMavenDescriptor(true);
  278. +        config.setPomPropertiesFile(new File("src/test/resources/pom.properties"));
  279. +
  280. +        archiver.createArchive( session, project, config );
  281. +        assertTrue( jarFile.exists() );
  282. +
  283. +        Properties properties = getJarFileMavenPomProperties(jarFile);
  284. +
  285. +        assertEquals(project.getArtifactId(), properties.getProperty("artifactId"));
  286. +        assertEquals(project.getGroupId(), properties.getProperty("groupId"));
  287. +        assertEquals(project.getVersion(), properties.getProperty("version"));
  288. +        assertEquals(project.getDescription(), properties.getProperty("description"));
  289. +        assertEquals("value", properties.getProperty("customProperty"));
  290. +    }
  291. +
  292. +    @SuppressWarnings( "ResultOfMethodCallIgnored" )
  293. +    public void testCustomMavenPropertiesTemplate()
  294. +        throws Exception
  295. +    {
  296. +      MavenSession session = getDummySession();
  297. +      MavenProject project = getDummyProject();
  298. +      File jarFile = new File( "target/test/dummy.jar" );
  299. +      JarArchiver jarArchiver = getCleanJarArciver( jarFile );
  300. +
  301. +      project.getProperties().setProperty("customProperty", "value");
  302. +
  303. +      MavenArchiver archiver = getMavenArchiver( jarArchiver );
  304. +
  305. +      MavenArchiveConfiguration config = new MavenArchiveConfiguration();
  306. +
  307. +      config.setAddMavenDescriptor(true);
  308. +      config.setPomPropertiesTemplate(
  309. +          "version=${project.version}\n" +
  310. +          "groupId=${project.groupId}\n" +
  311. +          "artifactId=${project.artifactId}\n" +
  312. +          "customProperty=${customProperty}");
  313. +
  314. +      archiver.createArchive( session, project, config );
  315. +      assertTrue( jarFile.exists() );
  316. +
  317. +      Properties properties = getJarFileMavenPomProperties(jarFile);
  318. +
  319. +      assertEquals(project.getArtifactId(), properties.getProperty("artifactId"));
  320. +      assertEquals(project.getGroupId(), properties.getProperty("groupId"));
  321. +      assertEquals(project.getVersion(), properties.getProperty("version"));
  322. +      assertEquals(project.getDescription(), properties.getProperty("description"));
  323. +      assertEquals("value", properties.getProperty("customProperty"));
  324. +    }
  325. +
  326. +    @SuppressWarnings( "ResultOfMethodCallIgnored" )
  327.      private void deleteAndAssertNotPresent( File jarFile )
  328.      {
  329.          jarFile.delete();
  330. @@ -1080,4 +1136,26 @@
  331.          }
  332.  
  333.      }
  334. +
  335. +    public Properties getJarFileMavenPomProperties(File jarFile)
  336. +        throws IOException
  337. +    {
  338. +        JarFile jar = null;
  339. +        try
  340. +        {
  341. +            jar = new JarFile( jarFile );
  342. +            InputStream propertiesFileStream = jar.getInputStream(jar.getEntry("META-INF/maven/org.apache.dummy/dummy/pom.properties"));
  343. +            Properties properties = new Properties();
  344. +            properties.load(propertiesFileStream);
  345. +            return properties;
  346. +        }
  347. +        finally
  348. +        {
  349. +            if ( jar != null )
  350. +            {
  351. +                jar.close();
  352. +            }
  353. +        }
  354. +
  355. +    }
  356.  }
  357. Index: src/test/resources/pom.properties
  358. ===================================================================
  359. --- src/test/resources/pom.properties   (revision 0)
  360. +++ src/test/resources/pom.properties   (working copy)
  361. @@ -0,0 +1,4 @@
  362. +version=$\{project.version\}
  363. +groupId=$\{project.groupId\}
  364. +artifactId=$\{project.artifactId\}
  365. +customProperty=$\{customProperty\}
  366. \ No newline at end of file
Add Comment
Please, Sign In to add comment