This is an automated email from the ASF dual-hosted git repository. michaelo pushed a commit to branch MSHARED-787 in repository https://gitbox.apache.org/repos/asf/maven-archiver.git
commit 64e8b56386e72ad976a4ebb8360c2697f9187823 Author: Michael Osipov <micha...@apache.org> AuthorDate: Wed Dec 26 00:37:25 2018 +0100 [MSHARED-787] Add optional buildEnvironment information to the manifest --- .../maven/archiver/ManifestConfiguration.java | 25 +++++++++++++++++++++- .../org/apache/maven/archiver/MavenArchiver.java | 22 +++++++++++++++---- .../apache/maven/archiver/MavenArchiverTest.java | 17 +++++++++++---- 3 files changed, 55 insertions(+), 9 deletions(-) diff --git a/src/main/java/org/apache/maven/archiver/ManifestConfiguration.java b/src/main/java/org/apache/maven/archiver/ManifestConfiguration.java index 7795c48..0f5e92a 100644 --- a/src/main/java/org/apache/maven/archiver/ManifestConfiguration.java +++ b/src/main/java/org/apache/maven/archiver/ManifestConfiguration.java @@ -57,6 +57,13 @@ public class ManifestConfiguration private String classpathPrefix = ""; /** + * Add build environment information about Maven, JDK, and OS. + * + * @since 3.3.1 + */ + private boolean addBuildEnvironmentEntries; + + /** * Add default implementation entries if this is an extension specification. * * @since 2.1 @@ -101,6 +108,14 @@ public class ManifestConfiguration } /** + * @return {@link #addBuildEnvironmentEntries} + */ + public boolean isAddBuildEnvironmentEntries() + { + return addBuildEnvironmentEntries; + } + + /** * @return {@link #addDefaultImplementationEntries} */ public boolean isAddDefaultImplementationEntries() @@ -133,6 +148,14 @@ public class ManifestConfiguration } /** + * @param addBuildEnvironmentEntries add build environment information true/false. + */ + public void setAddBuildEnvironmentEntries( boolean addBuildEnvironmentEntries ) + { + this.addBuildEnvironmentEntries = addBuildEnvironmentEntries; + } + + /** * @param addDefaultImplementationEntries true to add default implementations false otherwise. */ public void setAddDefaultImplementationEntries( boolean addDefaultImplementationEntries ) @@ -250,7 +273,7 @@ public class ManifestConfiguration * </ol> * <br> * <b>NOTE:</b> If you specify a layout type of 'custom' you MUST set this layout expression. - * You can take a look at + * You can take a look at * <ol> * <li>{@link MavenArchiver#SIMPLE_LAYOUT}</li> * <li>{@link MavenArchiver#SIMPLE_LAYOUT_NONUNIQUE}</li> diff --git a/src/main/java/org/apache/maven/archiver/MavenArchiver.java b/src/main/java/org/apache/maven/archiver/MavenArchiver.java index f925a39..c292a46 100644 --- a/src/main/java/org/apache/maven/archiver/MavenArchiver.java +++ b/src/main/java/org/apache/maven/archiver/MavenArchiver.java @@ -244,7 +244,10 @@ public class MavenArchiver // Added basic entries Manifest m = new Manifest(); - addCreatedByEntry( session, m, entries ); + if ( config.isAddBuildEnvironmentEntries() ) + { + handleBuildEnvironmentEntries( session, m, entries ); + } addCustomEntries( m, entries, config ); @@ -508,8 +511,6 @@ public class MavenArchiver private void addCustomEntries( Manifest m, Map<String, String> entries, ManifestConfiguration config ) throws ManifestException { - addManifestAttribute( m, entries, "Build-Jdk", System.getProperty( "java.version" ) ); - /* * TODO: rethink this, it wasn't working Artifact projectArtifact = project.getArtifact(); if ( * projectArtifact.isSnapshot() ) { Manifest.Attribute buildNumberAttr = new Manifest.Attribute( "Build-Number", @@ -615,6 +616,9 @@ public class MavenArchiver Manifest manifest = getManifest( session, workingProject, archiveConfiguration ); // Configure the jar + + // TODO here we would need to say archiver.setMinimalDefaultManifest(true) to avoid entry "Created-By: Plexus Achiver ${version}} + archiver.addConfiguredManifest( manifest ); archiver.setCompress( archiveConfiguration.isCompress() ); @@ -658,7 +662,7 @@ public class MavenArchiver } private void addCreatedByEntry( MavenSession session, Manifest m, Map<String, String> entries ) - throws ManifestException + throws ManifestException { String createdBy = "Apache Maven"; if ( session != null ) // can be null due to API backwards compatibility @@ -672,6 +676,16 @@ public class MavenArchiver addManifestAttribute( m, entries, "Created-By", createdBy ); } + private void handleBuildEnvironmentEntries( MavenSession session, Manifest m, Map<String, String> entries ) + throws ManifestException + { + addCreatedByEntry( session, m, entries ); + addManifestAttribute( m, entries, "Build-Jdk", String.format( "%s (%s)", System.getProperty( "java.version" ), + System.getProperty( "java.vm.vendor" ) ) ); + addManifestAttribute( m, entries, "Build-Os", String.format( "%s (%s; %s)", System.getProperty( "os.name" ), + System.getProperty( "os.version" ), System.getProperty( "os.arch" ) ) ); + } + private Artifact findArtifactWithFile( Set<Artifact> artifacts, File file ) { for ( Artifact artifact : artifacts ) diff --git a/src/test/java/org/apache/maven/archiver/MavenArchiverTest.java b/src/test/java/org/apache/maven/archiver/MavenArchiverTest.java index f4a496b..68c1c95 100644 --- a/src/test/java/org/apache/maven/archiver/MavenArchiverTest.java +++ b/src/test/java/org/apache/maven/archiver/MavenArchiverTest.java @@ -482,6 +482,7 @@ public class MavenArchiverTest config.setForced( true ); config.getManifest().setAddDefaultImplementationEntries( true ); config.getManifest().setAddDefaultSpecificationEntries( true ); + config.getManifest().setAddBuildEnvironmentEntries( true ); // noinspection deprecation MavenSession session = getDummySessionWithoutMavenVersion(); @@ -490,6 +491,11 @@ public class MavenArchiverTest Attributes manifest = getJarFileManifest( jarFile ).getMainAttributes(); assertEquals( "Apache Maven", manifest.get( new Attributes.Name( "Created-By" ) ) ); // no version number + assertEquals( String.format( "%s (%s)", System.getProperty( "java.version" ), + System.getProperty( "java.vm.vendor" )), manifest.get( new Attributes.Name( "Build-Jdk" ) ) ); + assertEquals( String.format( "%s (%s; %s)", System.getProperty( "os.name" ), + System.getProperty( "os.version" ), System.getProperty( "os.arch" )), + manifest.get( new Attributes.Name( "Build-Os" ) ) ); assertEquals( "archiver test", manifest.get( Attributes.Name.SPECIFICATION_TITLE ) ); assertEquals( "0.1", manifest.get( Attributes.Name.SPECIFICATION_VERSION ) ); @@ -498,8 +504,6 @@ public class MavenArchiverTest assertEquals( "archiver test", manifest.get( Attributes.Name.IMPLEMENTATION_TITLE ) ); assertEquals( "0.1.1", manifest.get( Attributes.Name.IMPLEMENTATION_VERSION ) ); assertEquals( "Apache", manifest.get( Attributes.Name.IMPLEMENTATION_VENDOR ) ); - - assertEquals( System.getProperty( "java.version" ), manifest.get( new Attributes.Name( "Build-Jdk" ) ) ); } @Test @@ -517,6 +521,7 @@ public class MavenArchiverTest config.setForced( true ); config.getManifest().setAddDefaultImplementationEntries( true ); config.getManifest().setAddDefaultSpecificationEntries( true ); + config.getManifest().setAddBuildEnvironmentEntries( true ); Map<String, String> manifestEntries = new HashMap<String, String>(); manifestEntries.put( "foo", "bar" ); @@ -539,6 +544,11 @@ public class MavenArchiverTest Attributes manifest = jarFileManifest.getMainAttributes(); assertEquals( "Apache Maven 3.0.4", manifest.get( new Attributes.Name( "Created-By" ) ) ); + assertEquals( String.format( "%s (%s)", System.getProperty( "java.version" ), + System.getProperty( "java.vm.vendor" )), manifest.get( new Attributes.Name( "Build-Jdk" ) ) ); + assertEquals( String.format( "%s (%s; %s)", System.getProperty( "os.name" ), + System.getProperty( "os.version" ), System.getProperty( "os.arch" )), + manifest.get( new Attributes.Name( "Build-Os" ) ) ); assertEquals( "archiver test", manifest.get( Attributes.Name.SPECIFICATION_TITLE ) ); assertEquals( "0.1", manifest.get( Attributes.Name.SPECIFICATION_VERSION ) ); @@ -554,8 +564,6 @@ public class MavenArchiverTest assertEquals( "olivier", manifest.get( new Attributes.Name( "first-name" ) ) ); assertEquals( "org.apache.maven.archiver", manifest.getValue( "Automatic-Module-Name" ) ); - assertEquals( System.getProperty( "java.version" ), manifest.get( new Attributes.Name( "Build-Jdk" ) ) ); - assertTrue( StringUtils.isEmpty( manifest.getValue( new Attributes.Name( "keyWithEmptyValue" ) ) ) ); assertTrue( manifest.containsKey( new Attributes.Name( "keyWithEmptyValue" ) ) ); @@ -605,6 +613,7 @@ public class MavenArchiverTest MavenArchiveConfiguration config = new MavenArchiveConfiguration(); config.setForced( true ); + config.getManifest().setAddBuildEnvironmentEntries( true ); archiver.createArchive( session, project, config ); assertTrue( jarFile.exists() );