Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Index: src/main/java/org/apache/maven/archiver/MavenArchiveConfiguration.java
- ===================================================================
- --- src/main/java/org/apache/maven/archiver/MavenArchiveConfiguration.java (revision 1523972)
- +++ src/main/java/org/apache/maven/archiver/MavenArchiveConfiguration.java (working copy)
- @@ -59,8 +59,9 @@
- * @since 2.3
- */
- private File pomPropertiesFile;
- + private String pomPropertiesTemplate;
- - public boolean isCompress()
- + public boolean isCompress()
- {
- return compress;
- }
- @@ -239,4 +240,14 @@
- {
- this.pomPropertiesFile = pomPropertiesFile;
- }
- +
- + public void setPomPropertiesTemplate(String template)
- + {
- + pomPropertiesTemplate = template;
- + }
- +
- + public String getPomPropertiesTemplate()
- + {
- + return pomPropertiesTemplate;
- + }
- }
- Index: src/main/java/org/apache/maven/archiver/MavenArchiver.java
- ===================================================================
- --- src/main/java/org/apache/maven/archiver/MavenArchiver.java (revision 1523972)
- +++ src/main/java/org/apache/maven/archiver/MavenArchiver.java (working copy)
- @@ -536,14 +536,16 @@
- // ----------------------------------------------------------------------
- // Create pom.properties file
- // ----------------------------------------------------------------------
- -
- - File pomPropertiesFile = archiveConfiguration.getPomPropertiesFile();
- - if ( pomPropertiesFile == null )
- + if (StringUtils.isNotEmpty(archiveConfiguration.getPomPropertiesTemplate()))
- {
- - File dir = new File( workingProject.getBuild().getDirectory(), "maven-archiver" );
- - pomPropertiesFile = new File( dir, "pom.properties" );
- + new PomPropertiesUtil().createPomProperties( workingProject, archiver,
- + archiveConfiguration.getPomPropertiesTemplate(), forced );
- }
- - new PomPropertiesUtil().createPomProperties( workingProject, archiver, pomPropertiesFile, forced );
- + else
- + {
- + File pomPropertiesTemplateFile = archiveConfiguration.getPomPropertiesFile();
- + new PomPropertiesUtil().createPomProperties( workingProject, archiver, pomPropertiesTemplateFile, forced );
- + }
- }
- // ----------------------------------------------------------------------
- Index: src/main/java/org/apache/maven/archiver/PomPropertiesUtil.java
- ===================================================================
- --- src/main/java/org/apache/maven/archiver/PomPropertiesUtil.java (revision 1523972)
- +++ src/main/java/org/apache/maven/archiver/PomPropertiesUtil.java (working copy)
- @@ -19,19 +19,15 @@
- * under the License.
- */
- -import java.io.File;
- -import java.io.FileInputStream;
- -import java.io.FileOutputStream;
- -import java.io.IOException;
- -import java.io.InputStream;
- -import java.io.OutputStream;
- -import java.util.Properties;
- -
- import org.apache.maven.project.MavenProject;
- +import org.apache.maven.shared.utils.StringUtils;
- +import org.apache.maven.shared.utils.io.IOUtil;
- import org.codehaus.plexus.archiver.Archiver;
- import org.codehaus.plexus.archiver.ArchiverException;
- -import org.apache.maven.shared.utils.io.IOUtil;
- +import java.io.*;
- +import java.util.Map;
- +import java.util.Properties;
- /**
- * This class is responsible for creating the pom.properties
- @@ -98,23 +94,129 @@
- /**
- * Creates the pom.properties file.
- */
- - public void createPomProperties( MavenProject project, Archiver archiver, File pomPropertiesFile,
- + public void createPomProperties( MavenProject project, Archiver archiver, File pomPropertiesTemplateFile,
- boolean forceCreation )
- throws ArchiverException, IOException
- {
- + String template = null;
- + if (pomPropertiesTemplateFile != null && pomPropertiesTemplateFile.exists())
- + {
- + InputStream templateFileStream = new FileInputStream(pomPropertiesTemplateFile);
- + try
- + {
- + template = IOUtil.toString(templateFileStream);
- + }
- + finally
- + {
- + IOUtil.close(templateFileStream);
- + }
- + }
- +
- + createPomProperties(project, archiver, template, forceCreation);
- + }
- +
- + /**
- + * Creates the pom.properties file.
- + */
- + public void createPomProperties( MavenProject project, Archiver archiver, String template,
- + boolean forceCreation )
- + throws ArchiverException, IOException
- + {
- final String artifactId = project.getArtifactId();
- final String groupId = project.getGroupId();
- - Properties p = new Properties();
- + File dir = new File( project.getBuild().getDirectory(), "maven-archiver" );
- + File pomPropertiesResultFile = new File( dir, "pom.properties" );
- - p.setProperty( "groupId", project.getGroupId() );
- + Properties p = getPreparedProperties(project, template);
- + createPropertyFile( p, pomPropertiesResultFile, forceCreation );
- - p.setProperty( "artifactId", project.getArtifactId() );
- + archiver.addFile( pomPropertiesResultFile, "META-INF/maven/" + groupId + "/" + artifactId + "/pom.properties" );
- + }
- - p.setProperty( "version", project.getVersion() );
- + /**
- + * Return properties for make pom.properties by template
- + *
- + * @param project Current project
- + * @param template Properties template file or null
- + * @return Properties
- + * @throws IOException If error read template file
- + */
- + private Properties getPreparedProperties(MavenProject project, String template) throws IOException {
- + Properties properties = new Properties();
- + if (template == null || StringUtils.isEmpty(template))
- + {
- + properties.setProperty( "groupId", project.getGroupId() );
- + properties.setProperty( "artifactId", project.getArtifactId() );
- + properties.setProperty( "version", project.getVersion() );
- + return properties;
- + }
- - createPropertyFile( p, pomPropertiesFile, forceCreation );
- + InputStream propertiesTemplateStream = null;
- + try
- + {
- + propertiesTemplateStream = new ByteArrayInputStream(template.getBytes());
- + properties.load(propertiesTemplateStream);
- - archiver.addFile( pomPropertiesFile, "META-INF/maven/" + groupId + "/" + artifactId + "/pom.properties" );
- + fillProperties(project, properties);
- +
- + return properties;
- + }
- + catch (FileNotFoundException e)
- + {
- + throw new IOException(e.getMessage(), e);
- + }
- + finally
- + {
- + IOUtil.close(propertiesTemplateStream);
- + }
- }
- +
- + /**
- + * Fill property values with values from project data
- + *
- + * @param project Current project
- + * @param properties Work properties
- + */
- + private void fillProperties(MavenProject project, Properties properties) {
- + for (Map.Entry<Object, Object> property : properties.entrySet())
- + {
- + if (property.getValue() == null)
- + {
- + continue;
- + }
- +
- + String propertyValue = property.getValue().toString();
- + if (propertyValue.startsWith("${"))
- + {
- + String projectProperty = propertyValue.replace("${", "").replace("}", "");
- + if (StringUtils.isEmpty(projectProperty))
- + {
- + continue;
- + }
- +
- + // todo: use velocity engine
- + if (project.getProperties().containsKey(projectProperty))
- + {
- + property.setValue(project.getProperties().getProperty(projectProperty));
- + }
- + else if ("project.artifactId".equals(projectProperty))
- + {
- + property.setValue(project.getArtifactId());
- + }
- + else if ("project.groupId".equals(projectProperty))
- + {
- + property.setValue(project.getGroupId());
- + }
- + else if ("project.version".equals(projectProperty))
- + {
- + property.setValue(project.getVersion());
- + }
- + else if ("project.description".equals(projectProperty))
- + {
- + property.setValue(project.getDescription());
- + }
- + }
- + }
- + }
- }
- Index: src/test/java/org/apache/maven/archiver/MavenArchiverTest.java
- ===================================================================
- --- src/test/java/org/apache/maven/archiver/MavenArchiverTest.java (revision 1523972)
- +++ src/test/java/org/apache/maven/archiver/MavenArchiverTest.java (working copy)
- @@ -33,26 +33,18 @@
- import org.apache.maven.monitor.event.EventDispatcher;
- import org.apache.maven.project.MavenProject;
- import org.apache.maven.settings.Settings;
- +import org.apache.maven.shared.utils.StringUtils;
- +import org.apache.maven.shared.utils.io.FileUtils;
- import org.codehaus.plexus.PlexusContainer;
- import org.codehaus.plexus.archiver.jar.JarArchiver;
- import org.codehaus.plexus.archiver.jar.ManifestException;
- -import org.apache.maven.shared.utils.io.FileUtils;
- -import org.apache.maven.shared.utils.StringUtils;
- import java.io.File;
- import java.io.IOException;
- +import java.io.InputStream;
- import java.net.URI;
- import java.net.URL;
- -import java.util.ArrayList;
- -import java.util.Collections;
- -import java.util.Comparator;
- -import java.util.Date;
- -import java.util.HashMap;
- -import java.util.List;
- -import java.util.Map;
- -import java.util.Properties;
- -import java.util.Set;
- -import java.util.TreeSet;
- +import java.util.*;
- import java.util.jar.Attributes;
- import java.util.jar.JarFile;
- import java.util.jar.Manifest;
- @@ -603,6 +595,70 @@
- }
- @SuppressWarnings( "ResultOfMethodCallIgnored" )
- + public void testCustomMavenPropertiesFile()
- + throws Exception
- + {
- + MavenSession session = getDummySession();
- + MavenProject project = getDummyProject();
- + File jarFile = new File( "target/test/dummy.jar" );
- + JarArchiver jarArchiver = getCleanJarArciver( jarFile );
- +
- + project.getProperties().setProperty("customProperty", "value");
- +
- + MavenArchiver archiver = getMavenArchiver( jarArchiver );
- +
- + MavenArchiveConfiguration config = new MavenArchiveConfiguration();
- +
- + config.setAddMavenDescriptor(true);
- + config.setPomPropertiesFile(new File("src/test/resources/pom.properties"));
- +
- + archiver.createArchive( session, project, config );
- + assertTrue( jarFile.exists() );
- +
- + Properties properties = getJarFileMavenPomProperties(jarFile);
- +
- + assertEquals(project.getArtifactId(), properties.getProperty("artifactId"));
- + assertEquals(project.getGroupId(), properties.getProperty("groupId"));
- + assertEquals(project.getVersion(), properties.getProperty("version"));
- + assertEquals(project.getDescription(), properties.getProperty("description"));
- + assertEquals("value", properties.getProperty("customProperty"));
- + }
- +
- + @SuppressWarnings( "ResultOfMethodCallIgnored" )
- + public void testCustomMavenPropertiesTemplate()
- + throws Exception
- + {
- + MavenSession session = getDummySession();
- + MavenProject project = getDummyProject();
- + File jarFile = new File( "target/test/dummy.jar" );
- + JarArchiver jarArchiver = getCleanJarArciver( jarFile );
- +
- + project.getProperties().setProperty("customProperty", "value");
- +
- + MavenArchiver archiver = getMavenArchiver( jarArchiver );
- +
- + MavenArchiveConfiguration config = new MavenArchiveConfiguration();
- +
- + config.setAddMavenDescriptor(true);
- + config.setPomPropertiesTemplate(
- + "version=${project.version}\n" +
- + "groupId=${project.groupId}\n" +
- + "artifactId=${project.artifactId}\n" +
- + "customProperty=${customProperty}");
- +
- + archiver.createArchive( session, project, config );
- + assertTrue( jarFile.exists() );
- +
- + Properties properties = getJarFileMavenPomProperties(jarFile);
- +
- + assertEquals(project.getArtifactId(), properties.getProperty("artifactId"));
- + assertEquals(project.getGroupId(), properties.getProperty("groupId"));
- + assertEquals(project.getVersion(), properties.getProperty("version"));
- + assertEquals(project.getDescription(), properties.getProperty("description"));
- + assertEquals("value", properties.getProperty("customProperty"));
- + }
- +
- + @SuppressWarnings( "ResultOfMethodCallIgnored" )
- private void deleteAndAssertNotPresent( File jarFile )
- {
- jarFile.delete();
- @@ -1080,4 +1136,26 @@
- }
- }
- +
- + public Properties getJarFileMavenPomProperties(File jarFile)
- + throws IOException
- + {
- + JarFile jar = null;
- + try
- + {
- + jar = new JarFile( jarFile );
- + InputStream propertiesFileStream = jar.getInputStream(jar.getEntry("META-INF/maven/org.apache.dummy/dummy/pom.properties"));
- + Properties properties = new Properties();
- + properties.load(propertiesFileStream);
- + return properties;
- + }
- + finally
- + {
- + if ( jar != null )
- + {
- + jar.close();
- + }
- + }
- +
- + }
- }
- Index: src/test/resources/pom.properties
- ===================================================================
- --- src/test/resources/pom.properties (revision 0)
- +++ src/test/resources/pom.properties (working copy)
- @@ -0,0 +1,4 @@
- +version=$\{project.version\}
- +groupId=$\{project.groupId\}
- +artifactId=$\{project.artifactId\}
- +customProperty=$\{customProperty\}
- \ No newline at end of file
Add Comment
Please, Sign In to add comment