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 2d575325e83ad11c11f145b260152378c4edcea8 Author: Michael Osipov <micha...@apache.org> AuthorDate: Sun Jan 27 19:27:55 2019 +0100 [MSHARED-787] Add optional buildEnvironment information to the manifest --- .../maven/archiver/ManifestConfiguration.java | 25 +++++++++++++++++++++- .../org/apache/maven/archiver/MavenArchiver.java | 19 ++++++++++++++++ src/site/apt/examples/manifest.apt | 4 ++-- src/site/xdoc/index.xml.vm | 20 +++++++++++++---- .../apache/maven/archiver/MavenArchiverTest.java | 11 ++++++++++ 5 files changed, 72 insertions(+), 7 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..51078d1 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.4.0 + */ + 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 ce4d36e..e3c89c2 100644 --- a/src/main/java/org/apache/maven/archiver/MavenArchiver.java +++ b/src/main/java/org/apache/maven/archiver/MavenArchiver.java @@ -248,6 +248,11 @@ public class MavenArchiver addCustomEntries( m, entries, config ); + if ( config.isAddBuildEnvironmentEntries() ) + { + handleBuildEnvironmentEntries( session, m, entries ); + } + if ( config.isAddClasspath() ) { StringBuilder classpath = new StringBuilder(); @@ -615,6 +620,9 @@ public class MavenArchiver Manifest manifest = getManifest( session, workingProject, archiveConfiguration ); // Configure the jar + + archiver.setMinimalDefaultManifest( true ); + archiver.addConfiguredManifest( manifest ); archiver.setCompress( archiveConfiguration.isCompress() ); @@ -672,6 +680,17 @@ public class MavenArchiver addManifestAttribute( m, entries, "Created-By", createdBy ); } + private void handleBuildEnvironmentEntries( MavenSession session, Manifest m, Map<String, String> entries ) + throws ManifestException + { + addManifestAttribute( m, entries, "Build-Tool", + session != null ? session.getSystemProperties().getProperty( "maven.build.version" ) : "Apache Maven" ); + addManifestAttribute( m, entries, "Build-Jdk", String.format( "%s (%s)", System.getProperty( "java.version" ), + System.getProperty( "java.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/site/apt/examples/manifest.apt b/src/site/apt/examples/manifest.apt index 0f0c108..ef2f1cd 100644 --- a/src/site/apt/examples/manifest.apt +++ b/src/site/apt/examples/manifest.apt @@ -36,8 +36,8 @@ Created-By: Apache Maven ${maven.version} Build-Jdk: ${java.version} +-----+ -<<Note:>> The <<<Build-Jdk>>> does not take toolchains configuration into account. It is the same -JDK version as running the Maven instance. + <<Note:>> The <<<Build-Jdk>>> does not take toolchains configuration into account. It is the same + JDK version as running the Maven instance. * Adding Implementation And Specification Details diff --git a/src/site/xdoc/index.xml.vm b/src/site/xdoc/index.xml.vm index 0bc7ff3..d7ce85f 100644 --- a/src/site/xdoc/index.xml.vm +++ b/src/site/xdoc/index.xml.vm @@ -43,6 +43,7 @@ <addClasspath/> <addDefaultImplementationEntries/> <addDefaultSpecificationEntries/> + <addBuildEnvironmentEntries/> <addExtensions/> <classpathLayoutType/> <classpathPrefix/> @@ -197,8 +198,7 @@ <source> Implementation-Title: \${project.name} Implementation-Version: \${project.version} -Implementation-Vendor: \${project.organization.name} - </source> +Implementation-Vendor: \${project.organization.name}</source> The default value is <code>false</code>. </td> <td>boolean</td> @@ -211,14 +211,26 @@ Implementation-Vendor: \${project.organization.name} <source> Specification-Title: \${project.name} Specification-Version: \${project.artifact.selectedVersion.majorVersion}.\${project.artifact.selectedVersion.minorVersion} -Specification-Vendor: \${project.organization.name} - </source> +Specification-Vendor: \${project.organization.name}</source> The default value is <code>false</code>. </td> <td>boolean</td> <td>2.1</td> </tr> <tr> + <td>addBuildEnvironmentEntries</td> + <td> + If the manifest will contain these entries: + <source> +Build-Tool: ${maven.build.version} +Build-Jdk: ${java.version} (${java.vendor}) +Build-Os: ${os.name} (${os.version}; (${os.arch})</source> + The default value is <code>false</code>. + </td> + <td>boolean</td> + <td>3.4.0</td> + </tr> + <tr> <td>addExtensions</td> <td> Whether to create an <code>Extension-List</code> manifest diff --git a/src/test/java/org/apache/maven/archiver/MavenArchiverTest.java b/src/test/java/org/apache/maven/archiver/MavenArchiverTest.java index 99c5214..2ef472e 100644 --- a/src/test/java/org/apache/maven/archiver/MavenArchiverTest.java +++ b/src/test/java/org/apache/maven/archiver/MavenArchiverTest.java @@ -518,6 +518,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" ); @@ -541,6 +542,14 @@ public class MavenArchiverTest assertEquals( "Apache Maven 3.0.4", manifest.get( new Attributes.Name( "Created-By" ) ) ); + assertEquals( session.getSystemProperties().get( "maven.build.version" ), + manifest.get( new Attributes.Name( "Build-Tool" ) ) ); + assertEquals( String.format( "%s (%s)", System.getProperty( "java.version" ), + System.getProperty( "java.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 ) ); assertEquals( "Apache", manifest.get( Attributes.Name.SPECIFICATION_VENDOR ) ); @@ -1410,6 +1419,8 @@ public class MavenArchiverTest { Properties systemProperties = new Properties(); systemProperties.put( "maven.version", "3.0.4" ); + systemProperties.put( "maven.build.version", + "Apache Maven 3.0.4 (3ad2b6794a8293a8ca6c1590708fb5d3fc795c49; 2012-01-17T08:39:41Z)" ); return getDummySession( systemProperties ); }