[MRRESOURCES-14, MRRESROUCES-15] Fix creation of target/.plxarc file, fix problems with SNAPSHOT dependencies


git-svn-id: https://svn.apache.org/repos/asf/maven/plugins/trunk@518239 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/src/main/java/org/apache/maven/plugin/resources/remote/ProcessRemoteResourcesMojo.java b/src/main/java/org/apache/maven/plugin/resources/remote/ProcessRemoteResourcesMojo.java
index f9bae2e..d31c204 100644
--- a/src/main/java/org/apache/maven/plugin/resources/remote/ProcessRemoteResourcesMojo.java
+++ b/src/main/java/org/apache/maven/plugin/resources/remote/ProcessRemoteResourcesMojo.java
@@ -19,6 +19,7 @@
  * under the License.
  */
 
+import org.apache.maven.artifact.factory.ArtifactFactory;
 import org.apache.maven.artifact.repository.ArtifactRepository;
 import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
 import org.apache.maven.artifact.Artifact;
@@ -37,21 +38,17 @@
 import org.apache.maven.model.Resource;
 import org.apache.velocity.VelocityContext;
 import org.codehaus.plexus.util.FileUtils;
-import org.codehaus.plexus.util.IOUtil;
 import org.codehaus.plexus.util.StringUtils;
 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
 import org.codehaus.plexus.velocity.VelocityComponent;
 
 import java.io.BufferedReader;
-import java.io.BufferedWriter;
 import java.io.File;
 import java.io.FileReader;
 import java.io.FileWriter;
 import java.io.IOException;
-import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.io.PrintWriter;
-import java.io.Writer;
 import java.net.MalformedURLException;
 import java.net.URL;
 import java.net.URLConnection;
@@ -168,7 +165,14 @@
      * @component
      */
     private ArtifactRepositoryFactory artifactRepositoryFactory;
-
+    
+    /**
+     * Artifact factory, needed to create artifacts.
+     *
+     * @component
+     */
+    private ArtifactFactory artifactFactory;
+    
     /**
      * The Maven session.
      *
@@ -178,7 +182,7 @@
     
     
    /**
-    * Artifact factory, needed to create projects from the artifacts.
+    * ProjectBuilder, needed to create projects from the artifacts.
     *
     * @component role="org.apache.maven.project.MavenProjectBuilder"
     * @required
@@ -227,17 +231,18 @@
                     position = bundleCount + "th";
                 }
 
-                throw new MojoExecutionException( "The " + position +
-                    " resource bundle configured must specify a groupId, artifactId, and version for a remote resource bundle. " +
-                    "Must be of the form <resourceBundle>groupId:artifactId:version</resourceBundle>" );
+                throw new MojoExecutionException( "The " + position
+                    + " resource bundle configured must specify a groupId, artifactId, and" 
+                    + " version for a remote resource bundle. " 
+                    + "Must be of the form <resourceBundle>groupId:artifactId:version</resourceBundle>" );
             }
 
             try
             {
                 File artifact = downloader.download( s[0], s[1], s[2], localRepository,
                                                      ProjectUtils.buildArtifactRepositories( remoteRepositories,
-                                                                                             artifactRepositoryFactory,
-                                                                                             mavenSession.getContainer() ) );
+                                                         artifactRepositoryFactory,
+                                                         mavenSession.getContainer() ) );
 
                 classLoader.addURL( artifact.toURI().toURL() );
             }
@@ -267,7 +272,7 @@
 
         InputStreamReader reader = null;
 
-        VelocityContext context = new VelocityContext(properties);
+        VelocityContext context = new VelocityContext( properties );
 
         context.put( "project", project );
         context.put( "projects", getProjects() );
@@ -299,7 +304,8 @@
 
                 reader = new InputStreamReader( conn.getInputStream() );
                                
-                try {
+                try 
+                {
 
                     RemoteResourcesBundleXpp3Reader bundleReader = new RemoteResourcesBundleXpp3Reader();
     
@@ -312,7 +318,7 @@
                         String projectResource = bundleResource;
     
     
-                        if ( projectResource.endsWith(".vm") )
+                        if ( projectResource.endsWith( ".vm" ) )
                         {
                             projectResource = projectResource.substring( 0, projectResource.length() - 3 );
                         }
@@ -323,14 +329,15 @@
     
                         FileUtils.mkdir( f.getParentFile().getAbsolutePath() );
     
-                        if ( !copyResourceIfExists(f, projectResource) )
+                        if ( !copyResourceIfExists( f, projectResource ) )
                         {
                             PrintWriter writer = new PrintWriter( new FileWriter( f ) );
     
-                            try {
+                            try 
+                            {
                                 velocity.getEngine().mergeTemplate( bundleResource, context, writer );
     
-                                File appendedResourceFile = new File( appendedResourcesDirectory, projectResource);
+                                File appendedResourceFile = new File( appendedResourcesDirectory, projectResource );
                                 if ( appendedResourceFile.exists() ) 
                                 {
                                     FileReader freader = new FileReader( appendedResourceFile );
@@ -344,12 +351,16 @@
                                         line = breader.readLine();
                                     }
                                 }
-                            } finally {
+                            }
+                            finally
+                            {
                                 writer.close();
                             }
                         }
                     }
-                } finally {
+                }
+                finally
+                {
                     reader.close();
                 }
             }
@@ -389,6 +400,8 @@
         {
             File dotFile = new File( project.getBuild().getDirectory(), ".plxarc" );
 
+            FileUtils.mkdir( dotFile.getParentFile().getAbsolutePath() );
+
             FileUtils.fileWrite( dotFile.getAbsolutePath(), outputDirectory.getName() );
         }
         catch ( IOException e )
@@ -404,22 +417,38 @@
         for ( Iterator it = project.getArtifacts().iterator() ; it.hasNext() ; ) 
         {
             Artifact artifact = (Artifact) it.next();
-            try {
-                MavenProject p = mavenProjectBuilder.buildFromRepository(artifact,
+            try 
+            {
+                if ( artifact.isSnapshot() )
+                {
+                    artifact = artifactFactory.createDependencyArtifact( artifact.getGroupId(),
+                                                                         artifact.getArtifactId(),
+                                                                         artifact.getVersionRange(),
+                                                                         artifact.getType(),
+                                                                         artifact.getClassifier(),
+                                                                         artifact.getScope(),
+                                                                         null,
+                                                                         artifact.isOptional() );
+                    artifact.setVersion( artifact.getBaseVersion() );
+                }
+                
+                getLog().debug("Building project for " + artifact);
+                MavenProject p = mavenProjectBuilder.buildFromRepository( artifact,
                                                         remoteRepositories,
                                                         localRepository,
-                                                        true);
-                projects.add(p);
-            } catch (ProjectBuildingException e) {
+                                                        true );
+                projects.add( p );
+            }
+            catch ( ProjectBuildingException e )
+            {
                 // TODO Auto-generated catch block
                 e.printStackTrace();
             }
-            
         }
         return projects;
     }
     
-    protected boolean copyResourceIfExists(File file, String relFileName)
+    protected boolean copyResourceIfExists( File file, String relFileName )
         throws IOException 
     {
         for ( Iterator i = resources.iterator(); i.hasNext(); )
@@ -432,16 +461,16 @@
                 continue;
             }
             //TODO - really should use the resource includes/excludes and name mapping
-            File source = new File(resourceDirectory, relFileName);
+            File source = new File( resourceDirectory, relFileName );
             
             if ( source.exists() 
-                && !source.equals(file))
+                && !source.equals( file ) )
             {
                 //TODO - should use filters here
-                FileUtils.copyFile(source, file);
+                FileUtils.copyFile( source, file );
                 
                 //exclude the original (so eclipse doesn't complain about duplicate resources)
-                resource.addExclude(relFileName);
+                resource.addExclude( relFileName );
                 
                 return true;
             }
diff --git a/src/test/java/org/apache/maven/plugin/resources/remote/RemoteResourcesMojoTest.java b/src/test/java/org/apache/maven/plugin/resources/remote/RemoteResourcesMojoTest.java
index 4c4a889..4da8544 100644
--- a/src/test/java/org/apache/maven/plugin/resources/remote/RemoteResourcesMojoTest.java
+++ b/src/test/java/org/apache/maven/plugin/resources/remote/RemoteResourcesMojoTest.java
@@ -24,29 +24,18 @@
 import org.apache.maven.artifact.repository.ArtifactRepository;
 import org.apache.maven.artifact.versioning.VersionRange;
 import org.apache.maven.execution.MavenSession;
-import org.apache.maven.execution.ReactorManager;
-import org.apache.maven.monitor.event.EventDispatcher;
-import org.apache.maven.plugin.MojoExecutionException;
 import org.apache.maven.plugin.resources.remote.stub.MavenProjectResourcesStub;
 import org.apache.maven.plugin.testing.AbstractMojoTestCase;
 import org.apache.maven.project.MavenProject;
-import org.apache.maven.settings.Settings;
-import org.codehaus.plexus.PlexusContainer;
 import org.codehaus.plexus.util.FileUtils;
 
 import java.io.File;
 import java.io.FileOutputStream;
-import java.io.IOException;
-import java.io.InputStream;
 import java.net.URL;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Calendar;
-import java.util.Date;
-import java.util.LinkedList;
-import java.util.List;
 import java.util.Properties;
-import java.util.jar.JarFile;
 import java.util.jar.JarOutputStream;
 import java.util.zip.ZipEntry;
 
@@ -102,7 +91,7 @@
     {
         buildResourceBundle( "default-createbundle",
                             new String[] { "SIMPLE.txt" },
-                            null);
+                            null );
     }
     
     public void testSimpleBundles()
@@ -112,20 +101,20 @@
         final ProcessRemoteResourcesMojo mojo = lookupProcessMojoWithSettings( project ,
                                                                         new String[] {
                                                                             "test:test:1.0"
-                                                                        });
+                                                                        } );
 
         setupDefaultProject( project );
         
         ArtifactRepository repo = (ArtifactRepository) getVariableValueFromObject( mojo, "localRepository" );
-        String path = repo.pathOf(new DefaultArtifact( "test",
+        String path = repo.pathOf( new DefaultArtifact( "test",
                                                         "test",
-                                                        VersionRange.createFromVersion("1.0"),
+                                                        VersionRange.createFromVersion( "1.0" ),
                                                         null,
                                                         "jar",
                                                         "",
-                                                        new DefaultArtifactHandler() ));
+                                                        new DefaultArtifactHandler() ) );
         
-        File file = new File(repo.getBasedir() + "/" + path + ".jar");
+        File file = new File( repo.getBasedir() + "/" + path + ".jar" );
         file.getParentFile().mkdirs();
         buildResourceBundle( "default-simplebundles-create",
                              new String[] { "SIMPLE.txt" },
@@ -135,8 +124,8 @@
         mojo.execute();
         
         file = (File) getVariableValueFromObject( mojo, "outputDirectory" );
-        file = new File(file, "SIMPLE.txt"); 
-        assertTrue(file.exists());
+        file = new File( file, "SIMPLE.txt" ); 
+        assertTrue( file.exists() );
     }
         
     public void testFilteredBundles()
@@ -146,20 +135,20 @@
         final ProcessRemoteResourcesMojo mojo = lookupProcessMojoWithSettings( project ,
                                                                         new String[] {
                                                                             "test:test:1.1"
-                                                                        });
+                                                                        } );
     
         setupDefaultProject( project );
         
         ArtifactRepository repo = (ArtifactRepository) getVariableValueFromObject( mojo, "localRepository" );
-        String path = repo.pathOf(new DefaultArtifact( "test",
+        String path = repo.pathOf( new DefaultArtifact( "test",
                                                         "test",
-                                                        VersionRange.createFromVersion("1.1"),
+                                                        VersionRange.createFromVersion( "1.1" ),
                                                         null,
                                                         "jar",
                                                         "",
-                                                        new DefaultArtifactHandler() ));
+                                                        new DefaultArtifactHandler() ) );
         
-        File file = new File(repo.getBasedir() + "/" + path + ".jar");
+        File file = new File( repo.getBasedir() + "/" + path + ".jar" );
         file.getParentFile().mkdirs();
         buildResourceBundle( "default-filterbundles-create",
                              new String[] { "FILTER.txt.vm" },
@@ -171,58 +160,58 @@
         mojo.execute();
         
         file = (File) getVariableValueFromObject( mojo, "outputDirectory" );
-        file = new File(file, "FILTER.txt"); 
-        assertTrue(file.exists());
+        file = new File( file, "FILTER.txt" ); 
+        assertTrue( file.exists() );
         
-        String data = FileUtils.fileRead(file);
-        assertTrue(data.indexOf("2007") != -1);
-        assertTrue(data.indexOf("default-filterbundles") != -1);
+        String data = FileUtils.fileRead( file );
+        assertTrue( data.indexOf( "2007" ) != -1 );
+        assertTrue( data.indexOf( "default-filterbundles" ) != -1 );
     }
 
-    protected void buildResourceBundle(String id,
+    protected void buildResourceBundle( String id,
                                        String resourceNames[],
-                                       File jarName)
+                                       File jarName )
     throws Exception 
     {
         final MavenProjectResourcesStub project = createTestProject( id );
 
-        final File resourceDir = new File(project.getBasedir() + "/src/main/resources");
+        final File resourceDir = new File( project.getBasedir() + "/src/main/resources" );
         final BundleRemoteResourcesMojo mojo = lookupBundleMojoWithSettings( project , resourceDir );
         
         setupDefaultProject( project );
         
-        for (int x = 0; x < resourceNames.length; x++)
+        for ( int x = 0; x < resourceNames.length; x++ )
         {
-            File resource = new File(resourceDir, resourceNames[x]);
-            URL source = getClass().getResource("/" + resourceNames[x]);
+            File resource = new File( resourceDir, resourceNames[x] );
+            URL source = getClass().getResource( "/" + resourceNames[x] );
         
-            FileUtils.copyURLToFile(source, resource);
+            FileUtils.copyURLToFile( source, resource );
         }
         
         mojo.execute();
         
-        File xmlFile = new File(project.getBasedir() + "/target/classes/META-INF/maven/remote-resources.xml");
-        assertTrue(xmlFile.exists());
+        File xmlFile = new File( project.getBasedir() + "/target/classes/META-INF/maven/remote-resources.xml" );
+        assertTrue( xmlFile.exists() );
         
-        String data = FileUtils.fileRead(xmlFile);
-        for (int x = 0; x < resourceNames.length; x++)
+        String data = FileUtils.fileRead( xmlFile );
+        for ( int x = 0; x < resourceNames.length; x++ )
         {
-            assertTrue(data.indexOf(resourceNames[x]) != -1);
+            assertTrue( data.indexOf( resourceNames[x] ) != -1 );
         }
         
-        if ( null != jarName)
+        if ( null != jarName )
         {
-            JarOutputStream jar = new JarOutputStream( new FileOutputStream(jarName) );
-            jar.putNextEntry(new ZipEntry("META-INF/maven/remote-resources.xml"));
-            jar.write(data.getBytes());
+            JarOutputStream jar = new JarOutputStream( new FileOutputStream( jarName ) );
+            jar.putNextEntry( new ZipEntry( "META-INF/maven/remote-resources.xml" ) );
+            jar.write( data.getBytes() );
             jar.closeEntry();
 
-            for (int x = 0; x < resourceNames.length; x++)
+            for ( int x = 0; x < resourceNames.length; x++ )
             {
-                File resource = new File(resourceDir, resourceNames[x]);
-                data = FileUtils.fileRead(resource);
-                jar.putNextEntry(new ZipEntry(resourceNames[x]));
-                jar.write(data.getBytes());
+                File resource = new File( resourceDir, resourceNames[x] );
+                data = FileUtils.fileRead( resource );
+                jar.putNextEntry( new ZipEntry( resourceNames[x] ) );
+                jar.write( data.getBytes() );
                 jar.closeEntry();
             }
             jar.close();
@@ -243,7 +232,7 @@
     {
         // put this on the root dir
         project.addFile( "pom.xml", MavenProjectResourcesStub.ROOT_FILE );
-        project.setInceptionYear("2007");
+        project.setInceptionYear( "2007" );
         // start creating the environment
         project.setupBuildEnvironment();
     }
@@ -262,17 +251,17 @@
     protected BundleRemoteResourcesMojo lookupBundleMojoWithDefaultSettings( final MavenProject project )
         throws Exception
     {
-        File resourceDir = new File(project.getBasedir() + "/src/main/resources");
-        return lookupBundleMojoWithSettings(project, resourceDir);
+        File resourceDir = new File( project.getBasedir() + "/src/main/resources" );
+        return lookupBundleMojoWithSettings( project, resourceDir );
     }
     protected BundleRemoteResourcesMojo lookupBundleMojoWithSettings( final MavenProject project,
-                                                                      File resourceDir)
+                                                                      File resourceDir )
     throws Exception
     {
         final BundleRemoteResourcesMojo mojo = lookupBundleMojo();
     
         setVariableValueToObject( mojo, "resourcesDirectory", resourceDir );
-        setVariableValueToObject( mojo, "outputDirectory", new File(project.getBuild().getOutputDirectory()) );
+        setVariableValueToObject( mojo, "outputDirectory", new File( project.getBuild().getOutputDirectory() ) );
         return mojo;
     }
 
@@ -289,14 +278,14 @@
 
 
     protected ProcessRemoteResourcesMojo lookupProcessMojoWithSettings( final MavenProject project,
-                                                                 String bundles[])
+                                                                 String bundles[] )
         throws Exception
     {
-        return lookupProcessMojoWithSettings( project, new ArrayList(Arrays.asList(bundles)));
+        return lookupProcessMojoWithSettings( project, new ArrayList( Arrays.asList( bundles ) ) );
     }
     
     protected ProcessRemoteResourcesMojo lookupProcessMojoWithSettings( final MavenProject project,
-                                                                 ArrayList bundles)
+                                                                 ArrayList bundles )
         throws Exception
     {
         final ProcessRemoteResourcesMojo mojo = lookupProcessMojo();
@@ -306,18 +295,18 @@
                                     null, //ArtifactRepository localRepository,
                                     null, //EventDispatcher eventDispatcher,
                                     null, //ReactorManager reactorManager, 
-                                    Arrays.asList(new String[] {"install"}),
+                                    Arrays.asList( new String[] {"install"} ),
                                     project.getBasedir().toString(),
                                     new Properties(),
-                                    Calendar.getInstance().getTime());
+                                    Calendar.getInstance().getTime() );
 
         
         setVariableValueToObject( mojo, "project", project );
-        setVariableValueToObject( mojo, "outputDirectory", new File(project.getBuild().getOutputDirectory()) );
+        setVariableValueToObject( mojo, "outputDirectory", new File( project.getBuild().getOutputDirectory() ) );
         setVariableValueToObject( mojo, "resourceBundles", bundles );
         setVariableValueToObject( mojo, "mavenSession", session );
-        setVariableValueToObject( mojo, "remoteRepositories", project.getRemoteArtifactRepositories());
-        setVariableValueToObject( mojo, "resources", project.getResources());
+        setVariableValueToObject( mojo, "remoteRepositories", project.getRemoteArtifactRepositories() );
+        setVariableValueToObject( mojo, "resources", project.getResources() );
         return mojo;
     }