You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by ti...@apache.org on 2019/07/27 13:46:29 UTC

[maven-archetype] 01/01: missing try-with-resources and createNewFile() before new FileOutputStream(f)

This is an automated email from the ASF dual-hosted git repository.

tibordigana pushed a commit to branch buildfix
in repository https://gitbox.apache.org/repos/asf/maven-archetype.git

commit 9aafaa049968e0c4f8bbcd28163c31caa86e4e51
Author: tibordigana <ti...@apache.org>
AuthorDate: Sat Jul 27 15:46:14 2019 +0200

    missing try-with-resources and createNewFile() before new FileOutputStream(f)
---
 .../maven/archetype/DefaultArchetypeManager.java   |  6 ++++
 .../maven/archetype/common/DefaultPomManager.java  |  6 ++++
 .../archetype/creator/FilesetArchetypeCreator.java | 33 ++++++++++++++++++----
 .../DefaultFilesetArchetypeGenerator.java          | 15 ++++++++--
 .../maven/archetype/old/DefaultOldArchetype.java   | 25 ++++++++++------
 .../apache/maven/archetype/old/ArchetypeTest.java  |  4 +--
 .../DefaultArchetypeCreationConfigurator.java      |  7 +++++
 7 files changed, 76 insertions(+), 20 deletions(-)

diff --git a/archetype-common/src/main/java/org/apache/maven/archetype/DefaultArchetypeManager.java b/archetype-common/src/main/java/org/apache/maven/archetype/DefaultArchetypeManager.java
index f58c233..e6dbbd5 100644
--- a/archetype-common/src/main/java/org/apache/maven/archetype/DefaultArchetypeManager.java
+++ b/archetype-common/src/main/java/org/apache/maven/archetype/DefaultArchetypeManager.java
@@ -96,6 +96,12 @@ public class DefaultArchetypeManager
             archive.getParentFile().mkdirs();
         }
 
+
+        if ( !archive.exists() && !archive.createNewFile() )
+        {
+            getLogger().error( "Could not create file " + archive.getPath() );
+        }
+
         try ( ZipOutputStream zos = new ZipOutputStream( new FileOutputStream( archive ) ) )
         {
             zos.setLevel( 9 );
diff --git a/archetype-common/src/main/java/org/apache/maven/archetype/common/DefaultPomManager.java b/archetype-common/src/main/java/org/apache/maven/archetype/common/DefaultPomManager.java
index ca30cd7..5ae345f 100644
--- a/archetype-common/src/main/java/org/apache/maven/archetype/common/DefaultPomManager.java
+++ b/archetype-common/src/main/java/org/apache/maven/archetype/common/DefaultPomManager.java
@@ -234,6 +234,12 @@ public class DefaultPomManager
             ioe.initCause( exc );
             throw ioe;
         }
+
+
+        if ( !pomFile.exists() && !pomFile.createNewFile() )
+        {
+            getLogger().error( "Could not create file " + pomFile.getPath() );
+        }
         
         try ( Writer outputStreamWriter = new OutputStreamWriter( new FileOutputStream( pomFile ), fileEncoding ) ) 
         {
diff --git a/archetype-common/src/main/java/org/apache/maven/archetype/creator/FilesetArchetypeCreator.java b/archetype-common/src/main/java/org/apache/maven/archetype/creator/FilesetArchetypeCreator.java
index f499d03..cd36d27 100644
--- a/archetype-common/src/main/java/org/apache/maven/archetype/creator/FilesetArchetypeCreator.java
+++ b/archetype-common/src/main/java/org/apache/maven/archetype/creator/FilesetArchetypeCreator.java
@@ -80,6 +80,8 @@ import org.codehaus.plexus.util.WriterFactory;
 import org.codehaus.plexus.util.xml.Xpp3Dom;
 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
 
+import static org.apache.commons.io.IOUtils.write;
+
 /**
  * Create a 2.x Archetype project from a project. Since 2.0-alpha-5, an integration-test named "basic" is created along
  * the archetype itself to provide immediate test when building the archetype.
@@ -327,8 +329,14 @@ public class FilesetArchetypeCreator
         File basicItDirectory = new File( generatedSourcesDirectory, basic );
         basicItDirectory.mkdirs();
 
+        File archetypePropertiesFile = new File( basicItDirectory, "archetype.properties" );
+        if ( !archetypePropertiesFile.exists() && !archetypePropertiesFile.createNewFile() )
+        {
+            getLogger().error( "Could not create file " + archetypePropertiesFile.getPath() );
+        }
+
         try ( InputStream in = FilesetArchetypeCreator.class.getResourceAsStream( "archetype.properties" );
-              OutputStream out = new FileOutputStream( new File( basicItDirectory, "archetype.properties" ) ) )
+              OutputStream out = new FileOutputStream( archetypePropertiesFile ) )
         {
             Properties archetypeProperties = new Properties();
             archetypeProperties.load( in );
@@ -461,6 +469,11 @@ public class FilesetArchetypeCreator
     private void copyResource( String name, File destination )
         throws IOException
     {
+        if ( !destination.exists() && !destination.createNewFile() )
+        {
+            getLogger().error( "Could not create file " + destination.getPath() );
+        }
+
         try ( InputStream in = FilesetArchetypeCreator.class.getResourceAsStream( name );
               OutputStream out = new FileOutputStream( destination ) )
         {
@@ -850,6 +863,8 @@ public class FilesetArchetypeCreator
 
             FileUtils.copyFile( initialPomFile, inputFile );
 
+            outputFile.getParentFile().mkdirs();
+
             try ( Reader in = ReaderFactory.newXmlReader( inputFile );
                   Writer out = WriterFactory.newXmlWriter( outputFile ) )
             {
@@ -857,8 +872,6 @@ public class FilesetArchetypeCreator
 
                 String content = getReversedContent( initialcontent, pomReversedProperties );
 
-                outputFile.getParentFile().mkdirs();
-
                 IOUtil.copy( content, out );
             }
 
@@ -1063,6 +1076,8 @@ public class FilesetArchetypeCreator
 
             FileUtils.copyFile( initialPomFile, inputFile );
 
+            outputFile.getParentFile().mkdirs();
+
             try ( Reader in = ReaderFactory.newXmlReader( inputFile );
                   Writer out = WriterFactory.newXmlWriter( outputFile ) )
             {
@@ -1070,8 +1085,6 @@ public class FilesetArchetypeCreator
 
                 String content = getReversedContent( initialcontent, pomReversedProperties );
 
-                outputFile.getParentFile().mkdirs();
-
                 IOUtil.copy( content, out );
             }
 
@@ -1288,7 +1301,15 @@ public class FilesetArchetypeCreator
             File outputFile = new File( archetypeFilesDirectory, outputFilename );
             outputFile.getParentFile().mkdirs();
 
-            org.apache.commons.io.IOUtils.write( content, new FileOutputStream( outputFile ), fileEncoding );
+            if ( !outputFile.exists() && !outputFile.createNewFile() )
+            {
+                getLogger().error( "Could not create file " + outputFile.getPath() );
+            }
+
+            try ( OutputStream os = new FileOutputStream( outputFile ) )
+            {
+                write( content, os, fileEncoding );
+            }
         }
     }
 
diff --git a/archetype-common/src/main/java/org/apache/maven/archetype/generator/DefaultFilesetArchetypeGenerator.java b/archetype-common/src/main/java/org/apache/maven/archetype/generator/DefaultFilesetArchetypeGenerator.java
index b4d22c0..790a982 100644
--- a/archetype-common/src/main/java/org/apache/maven/archetype/generator/DefaultFilesetArchetypeGenerator.java
+++ b/archetype-common/src/main/java/org/apache/maven/archetype/generator/DefaultFilesetArchetypeGenerator.java
@@ -284,6 +284,10 @@ public class DefaultFilesetArchetypeGenerator
         else
         {
             outFile.getParentFile().mkdirs();
+            if ( !outFile.exists() && !outFile.createNewFile() )
+            {
+                getLogger().error( "Could not create file " + outFile.getPath() );
+            }
 
             try ( InputStream inputStream = archetypeZipFile.getInputStream( input );
                   OutputStream out = new FileOutputStream( outFile ) )
@@ -496,7 +500,7 @@ public class DefaultFilesetArchetypeGenerator
     private int processFileSet( String directory, List<String> fileSetResources, boolean packaged, String packageName,
                                 Context context, File outputDirectoryFile, String moduleOffset,
                                 String archetypeEncoding, boolean failIfExists )
-        throws OutputFileExists, ArchetypeGenerationFailure
+        throws IOException, OutputFileExists, ArchetypeGenerationFailure
     {
         int count = 0;
 
@@ -602,7 +606,7 @@ public class DefaultFilesetArchetypeGenerator
     }
 
     private void processPom( Context context, File pom, String moduleOffset )
-        throws OutputFileExists, ArchetypeGenerationFailure
+        throws IOException, OutputFileExists, ArchetypeGenerationFailure
     {
         getLogger().debug( "Processing pom " + pom );
 
@@ -657,7 +661,7 @@ public class DefaultFilesetArchetypeGenerator
     @SuppressWarnings( "deprecation" )
     private boolean processTemplate( File outFile, Context context, String templateFileName, String encoding,
                                      boolean failIfExists )
-        throws OutputFileExists, ArchetypeGenerationFailure
+        throws IOException, OutputFileExists, ArchetypeGenerationFailure
     {
         templateFileName = templateFileName.replace( File.separatorChar, '/' );
 
@@ -696,6 +700,11 @@ public class DefaultFilesetArchetypeGenerator
             outFile.getParentFile().mkdirs();
         }
 
+        if ( !outFile.exists() && !outFile.createNewFile() )
+        {
+            getLogger().error( "Could not create file " + outFile.getPath() );
+        }
+
         getLogger().debug( "Merging into " + outFile );
 
         try ( Writer writer = new OutputStreamWriter( new FileOutputStream( outFile ), encoding ) )
diff --git a/archetype-common/src/main/java/org/apache/maven/archetype/old/DefaultOldArchetype.java b/archetype-common/src/main/java/org/apache/maven/archetype/old/DefaultOldArchetype.java
index 07f75b8..5e90fc5 100644
--- a/archetype-common/src/main/java/org/apache/maven/archetype/old/DefaultOldArchetype.java
+++ b/archetype-common/src/main/java/org/apache/maven/archetype/old/DefaultOldArchetype.java
@@ -305,6 +305,10 @@ public class DefaultOldArchetype
         {
             processTemplates( pomFile, outputDirectory, context, descriptor, packageName, parentModel );
         }
+        catch ( IOException e )
+        {
+            throw new ArchetypeTemplateProcessingException( "Unable to process template", e );
+        }
         finally
         {
             Thread.currentThread().setContextClassLoader( old );
@@ -395,7 +399,7 @@ public class DefaultOldArchetype
 
     private void processTemplates( File pomFile, String outputDirectory, Context context,
                                    ArchetypeDescriptor descriptor, String packageName, Model parentModel )
-        throws ArchetypeTemplateProcessingException
+            throws ArchetypeTemplateProcessingException, IOException
     {
         if ( !pomFile.exists() )
         {
@@ -616,7 +620,7 @@ public class DefaultOldArchetype
 
     private void processTemplate( String outputDirectory, Context context, String template,
                                   TemplateDescriptor descriptor, boolean packageInFileName, String packageName )
-        throws ArchetypeTemplateProcessingException
+            throws ArchetypeTemplateProcessingException, IOException
     {
         processTemplate( outputDirectory, context, template, descriptor, packageInFileName, packageName, null );
     }
@@ -633,7 +637,7 @@ public class DefaultOldArchetype
 
     protected void processSources( String outputDirectory, Context context, ArchetypeDescriptor descriptor,
                                    String packageName, String sourceDirectory )
-        throws ArchetypeTemplateProcessingException
+            throws ArchetypeTemplateProcessingException, IOException
     {
         for ( String template : descriptor.getSources() )
         {
@@ -644,7 +648,7 @@ public class DefaultOldArchetype
 
     protected void processTestSources( String outputDirectory, Context context, ArchetypeDescriptor descriptor,
                                        String packageName, String testSourceDirectory )
-        throws ArchetypeTemplateProcessingException
+            throws ArchetypeTemplateProcessingException, IOException
     {
         for ( String template : descriptor.getTestSources() )
         {
@@ -655,7 +659,7 @@ public class DefaultOldArchetype
 
     protected void processResources( String outputDirectory, Context context, ArchetypeDescriptor descriptor,
                                      String packageName )
-        throws ArchetypeTemplateProcessingException
+        throws IOException, ArchetypeTemplateProcessingException
     {
         for ( String template : descriptor.getResources() )
         {
@@ -666,7 +670,7 @@ public class DefaultOldArchetype
 
     protected void processTestResources( String outputDirectory, Context context, ArchetypeDescriptor descriptor,
                                          String packageName )
-        throws ArchetypeTemplateProcessingException
+        throws IOException, ArchetypeTemplateProcessingException
     {
         for ( String template : descriptor.getTestResources() )
         {
@@ -677,7 +681,7 @@ public class DefaultOldArchetype
 
     protected void processSiteResources( String outputDirectory, Context context, ArchetypeDescriptor descriptor,
                                          String packageName )
-        throws ArchetypeTemplateProcessingException
+        throws IOException, ArchetypeTemplateProcessingException
     {
         for ( String template : descriptor.getSiteResources() )
         {
@@ -689,7 +693,7 @@ public class DefaultOldArchetype
     protected void processTemplate( String outputDirectory, Context context, String template,
                                     TemplateDescriptor descriptor, boolean packageInFileName, String packageName,
                                     String sourceDirectory )
-        throws ArchetypeTemplateProcessingException
+        throws IOException, ArchetypeTemplateProcessingException
     {
         File f;
 
@@ -739,6 +743,11 @@ public class DefaultOldArchetype
             f.getParentFile().mkdirs();
         }
 
+        if ( !f.exists() && !f.createNewFile() )
+        {
+            getLogger().error( "Could not create file " + f.getPath() );
+        }
+
         if ( descriptor.isFiltered() )
         {
             try ( Writer writer = new OutputStreamWriter( new FileOutputStream( f ), descriptor.getEncoding() ) )
diff --git a/archetype-common/src/test/java/org/apache/maven/archetype/old/ArchetypeTest.java b/archetype-common/src/test/java/org/apache/maven/archetype/old/ArchetypeTest.java
index 92f2fd5..5897695 100644
--- a/archetype-common/src/test/java/org/apache/maven/archetype/old/ArchetypeTest.java
+++ b/archetype-common/src/test/java/org/apache/maven/archetype/old/ArchetypeTest.java
@@ -172,10 +172,8 @@ public class ArchetypeTest
         File artifactDir = getTestFile( "target", (String) parameters.get( "artifactId" ) );
         File pomFile = getTestFile( artifactDir.getAbsolutePath(), OldArchetype.ARCHETYPE_POM );
 
-        try
+        try ( FileReader pomReader = new FileReader( pomFile ) )
         {
-            FileReader pomReader = new FileReader( pomFile );
-
             MavenXpp3Reader reader = new MavenXpp3Reader();
 
             generatedModel = reader.read( pomReader );
diff --git a/maven-archetype-plugin/src/main/java/org/apache/maven/archetype/ui/creation/DefaultArchetypeCreationConfigurator.java b/maven-archetype-plugin/src/main/java/org/apache/maven/archetype/ui/creation/DefaultArchetypeCreationConfigurator.java
index 3c5f9e9..2a8a4fb 100644
--- a/maven-archetype-plugin/src/main/java/org/apache/maven/archetype/ui/creation/DefaultArchetypeCreationConfigurator.java
+++ b/maven-archetype-plugin/src/main/java/org/apache/maven/archetype/ui/creation/DefaultArchetypeCreationConfigurator.java
@@ -309,6 +309,13 @@ public class DefaultArchetypeCreationConfigurator
             storedProperties.setProperty( propertyKey, properties.getProperty( propertyKey ) );
         }
 
+        propertyFile.getParentFile().mkdirs();
+
+        if ( !propertyFile.exists() && !propertyFile.createNewFile() )
+        {
+            getLogger().error( "Could not create file " + propertyFile.getPath() );
+        }
+
         try ( OutputStream os = new FileOutputStream( propertyFile ) )
         {
             storedProperties.store( os, "" );