Skip to content

Commit 614b147

Browse files
committed
[FELIX-6187] Avoid overwriting the manifest if it has not changed
git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1867778 13f79535-47bb-0310-9956-ffa450edef68
1 parent c9a2c02 commit 614b147

1 file changed

Lines changed: 27 additions & 20 deletions

File tree

tools/maven-bundle-plugin/src/main/java/org/apache/felix/bundleplugin/ManifestPlugin.java

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
package org.apache.felix.bundleplugin;
2020

2121

22+
import java.io.ByteArrayOutputStream;
2223
import java.io.File;
2324
import java.io.FileInputStream;
2425
import java.io.FileNotFoundException;
@@ -31,6 +32,7 @@
3132
import java.nio.file.Files;
3233
import java.nio.file.Path;
3334
import java.nio.file.Paths;
35+
import java.util.Arrays;
3436
import java.util.Iterator;
3537
import java.util.LinkedHashMap;
3638
import java.util.List;
@@ -41,7 +43,6 @@
4143
import java.util.jar.Manifest;
4244
import java.util.stream.Collectors;
4345
import java.util.stream.Stream;
44-
import java.util.zip.ZipEntry;
4546
import java.util.zip.ZipFile;
4647

4748
import aQute.bnd.osgi.Analyzer;
@@ -64,6 +65,8 @@
6465
import org.osgi.service.metatype.MetaTypeService;
6566
import org.sonatype.plexus.build.incremental.BuildContext;
6667

68+
import static aQute.lib.strings.Strings.join;
69+
6770

6871
/**
6972
* Generate an OSGi manifest for this project
@@ -87,6 +90,12 @@ public class ManifestPlugin extends BundlePlugin
8790
@Parameter( property = "supportIncrementalBuild" )
8891
private boolean supportIncrementalBuild;
8992

93+
/**
94+
* When true, show stale files in the log at info level else at debug level.
95+
*/
96+
@Parameter( property = "showStaleFiles" )
97+
private boolean showStaleFiles;
98+
9099
@Component
91100
private BuildContext buildContext;
92101

@@ -377,9 +386,10 @@ private boolean isUpToDate(MavenProject project) throws MojoExecutionException {
377386
.collect(Collectors.toSet());
378387
if (!stale.isEmpty()) {
379388
getLog().info("Stale files detected, re-generating manifest.");
380-
if (getLog().isDebugEnabled()) {
381-
getLog().debug("Stale files: " + stale.stream()
382-
.collect(Collectors.joining(", ")));
389+
if (showStaleFiles) {
390+
getLog().info("Stale files: " + join(", ", stale));
391+
} else if (getLog().isDebugEnabled()) {
392+
getLog().debug("Stale files: " + join(", ", stale));
383393
}
384394
} else {
385395
// everything is in order, skip
@@ -460,15 +470,10 @@ public static void writeManifest( Analyzer analyzer, File outputFile, boolean ni
460470
{
461471
Manifest analyzerManifest = manifest;
462472
manifest = new Manifest();
463-
InputStream inputStream = new FileInputStream( outputFile );
464-
try
473+
try( InputStream inputStream = new FileInputStream( outputFile ) )
465474
{
466475
manifest.read( inputStream );
467476
}
468-
finally
469-
{
470-
inputStream.close();
471-
}
472477
Instructions instructions = new Instructions( ExtList.from( analyzer.getProperty("Merge-Headers") ) );
473478
mergeManifest( instructions, manifest, analyzerManifest );
474479
}
@@ -492,20 +497,22 @@ public static void writeManifest( Manifest manifest, File outputFile, boolean ni
492497
log.debug("Write manifest to " + outputFile.getPath());
493498
outputFile.getParentFile().mkdirs();
494499

495-
OutputStream os = buildContext.newFileOutputStream( outputFile );
496-
try
500+
try ( ByteArrayOutputStream baos = new ByteArrayOutputStream() )
497501
{
498-
ManifestWriter.outputManifest( manifest, os, niceManifest );
499-
}
500-
finally
501-
{
502-
try
502+
ManifestWriter.outputManifest( manifest, baos, niceManifest );
503+
baos.flush();
504+
byte[] newdata = baos.toByteArray();
505+
byte[] curdata = new byte[0];
506+
if (outputFile.exists())
503507
{
504-
os.close();
508+
curdata = Files.readAllBytes( outputFile.toPath() );
505509
}
506-
catch ( IOException e )
510+
if ( !Arrays.equals( newdata, curdata ) )
507511
{
508-
// nothing we can do here
512+
try ( OutputStream os = buildContext.newFileOutputStream( outputFile ) )
513+
{
514+
os.write( curdata );
515+
}
509516
}
510517
}
511518
}

0 commit comments

Comments
 (0)