Author: vsiveton
Date: Fri Dec 17 13:44:20 2010
New Revision: 1050381

URL: http://svn.apache.org/viewvc?rev=1050381&view=rev
Log:
MDOAP-32: Generate a DOAP file for another Maven Project

o added test case

Modified:
    
maven/plugins/trunk/maven-doap-plugin/src/main/java/org/apache/maven/plugin/doap/DoapMojo.java
    maven/plugins/trunk/maven-doap-plugin/src/site/apt/usage.apt.vm
    
maven/plugins/trunk/maven-doap-plugin/src/test/java/org/apache/maven/plugin/doap/DoapMojoTest.java

Modified: 
maven/plugins/trunk/maven-doap-plugin/src/main/java/org/apache/maven/plugin/doap/DoapMojo.java
URL: 
http://svn.apache.org/viewvc/maven/plugins/trunk/maven-doap-plugin/src/main/java/org/apache/maven/plugin/doap/DoapMojo.java?rev=1050381&r1=1050380&r2=1050381&view=diff
==============================================================================
--- 
maven/plugins/trunk/maven-doap-plugin/src/main/java/org/apache/maven/plugin/doap/DoapMojo.java
 (original)
+++ 
maven/plugins/trunk/maven-doap-plugin/src/main/java/org/apache/maven/plugin/doap/DoapMojo.java
 Fri Dec 17 13:44:20 2010
@@ -173,7 +173,7 @@ public class DoapMojo
      * @required
      * @since 1.1
      */
-    private File outputDirectory;
+    private String outputDirectory;
 
     /**
      * The local repository where the artifacts are located.
@@ -362,61 +362,106 @@ public class DoapMojo
     // ----------------------------------------------------------------------
 
     /** {...@inheritdoc} */
-    @SuppressWarnings( "unchecked" )
     public void execute()
         throws MojoExecutionException
     {
-        MavenProject givenProject = project;
+        // single artifact
+        if ( artifact != null )
+        {
+            MavenProject givenProject = getMavenProject( artifact );
+            if ( givenProject != null )
+            {
+                File outFile = new File( outputDirectory, 
artifact.getDoapFileName() );
+                writeDoapFile( givenProject, outFile );
+                return;
+            }
+        }
+
+        // current project
         File outFile = new File( doapFile );
         if ( !doapFile.contains( File.separator ) )
         {
             outFile = new File( outputDirectory, doapFile );
         }
+        writeDoapFile( project, outFile );
+    }
+
+    // ----------------------------------------------------------------------
+    // Private methods
+    // ----------------------------------------------------------------------
 
-        if ( artifact != null && StringUtils.isNotEmpty( artifact.getGroupId() 
)
-            && StringUtils.isNotEmpty( artifact.getArtifactId() ) && 
StringUtils.isNotEmpty( artifact.getVersion() ) )
+    /**
+     * @param artifact not null
+     * @return the maven project for the given doap artifact
+     * @since 1.1
+     */
+    private MavenProject getMavenProject( DoapArtifact artifact )
+    {
+        if ( artifact == null )
         {
-            getLog().info( "Using artifact " + artifact.getGroupId() + ":" + 
artifact.getArtifactId() + ":"
-                               + artifact.getVersion() );
+            return null;
+        }
 
-            try
-            {
-                Artifact art =
-                    factory.createProjectArtifact( artifact.getGroupId(), 
artifact.getArtifactId(),
-                                                   artifact.getVersion(), 
Artifact.SCOPE_COMPILE );
+        if ( StringUtils.isEmpty( artifact.getGroupId() ) || 
StringUtils.isEmpty( artifact.getArtifactId() )
+            || StringUtils.isEmpty( artifact.getVersion() ) )
+        {
+            getLog().warn( "Missing groupId or artifactId or version in 
<artifact/> parameter, ignored it" );
+            return null;
+        }
 
-                if ( art.getFile() == null )
-                {
-                    givenProject = mavenProjectBuilder.buildFromRepository( 
art, remoteRepositories, localRepository );
-                    art = givenProject.getArtifact();
+        getLog().info( "Using artifact " + artifact.getGroupId() + ":" + 
artifact.getArtifactId() + ":"
+                           + artifact.getVersion() );
 
-                    resolver.resolve( art, remoteRepositories, localRepository 
);
-                }
+        try
+        {
+            Artifact art =
+                factory.createProjectArtifact( artifact.getGroupId(), 
artifact.getArtifactId(), artifact.getVersion(),
+                                               Artifact.SCOPE_COMPILE );
 
-                outFile = new File( outputDirectory, 
artifact.getDoapFileName() );
-            }
-            catch ( ArtifactResolutionException e )
+            if ( art.getFile() == null )
             {
-                getLog().error( "ArtifactResolutionException: " + 
e.getMessage() );
-                getLog().warn( "Ignored <artifact/> parameter." );
-            }
-            catch ( ArtifactNotFoundException e )
-            {
-                getLog().error( "ArtifactNotFoundException: " + e.getMessage() 
);
-                getLog().warn( "Ignored <artifact/> parameter." );
-            }
-            catch ( ProjectBuildingException e )
-            {
-                getLog().error( "ProjectBuildingException: " + e.getMessage() 
);
-                getLog().warn( "Ignored <artifact/> parameter." );
+                MavenProject proj = mavenProjectBuilder.buildFromRepository( 
art, remoteRepositories, localRepository );
+                art = proj.getArtifact();
+
+                resolver.resolve( art, remoteRepositories, localRepository );
+
+                return proj;
             }
         }
+        catch ( ArtifactResolutionException e )
+        {
+            getLog().error( "ArtifactResolutionException: " + e.getMessage() );
+            getLog().warn( "Ignored <artifact/> parameter." );
+        }
+        catch ( ArtifactNotFoundException e )
+        {
+            getLog().error( "ArtifactNotFoundException: " + e.getMessage() );
+            getLog().warn( "Ignored <artifact/> parameter." );
+        }
+        catch ( ProjectBuildingException e )
+        {
+            getLog().error( "ProjectBuildingException: " + e.getMessage() );
+            getLog().warn( "Ignored <artifact/> parameter." );
+        }
+
+        return null;
+    }
 
+    /**
+     * Write a doap file for the given project.
+     *
+     * @param project not null
+     * @param outputFile not null
+     * @since 1.1
+     */
+    private void writeDoapFile( MavenProject project, File outputFile )
+        throws MojoExecutionException
+    {
         // 
----------------------------------------------------------------------------
         // Includes ASF extensions
         // 
----------------------------------------------------------------------------
 
-        if ( !asfExtOptions.isIncluded() && ASFExtOptions.isASFProject( 
givenProject ) )
+        if ( !asfExtOptions.isIncluded() && ASFExtOptions.isASFProject( 
project ) )
         {
             getLog().info( "This project is an ASF project, ASF Extensions to 
DOAP will be added." );
             asfExtOptions.setIncluded( true );
@@ -429,28 +474,28 @@ public class DoapMojo
         Writer w;
         try
         {
-            if ( !outFile.getParentFile().exists() )
+            if ( !outputFile.getParentFile().exists() )
             {
-                FileUtils.mkdir( outFile.getParentFile().getAbsolutePath() );
+                FileUtils.mkdir( outputFile.getParentFile().getAbsolutePath() 
);
             }
 
-            w = WriterFactory.newXmlWriter( outFile );
+            w = WriterFactory.newXmlWriter( outputFile );
         }
         catch ( IOException e )
         {
-            throw new MojoExecutionException( "Error creating DOAP file " + 
outFile.getAbsolutePath(), e );
+            throw new MojoExecutionException( "Error creating DOAP file " + 
outputFile.getAbsolutePath(), e );
         }
 
         if ( asfExtOptions.isIncluded() )
         {
-            getLog().info( "Generating an ASF DOAP file " + 
outFile.getAbsolutePath() );
+            getLog().info( "Generating an ASF DOAP file " + 
outputFile.getAbsolutePath() );
         }
         else
         {
-            getLog().info( "Generating a pure DOAP file " + 
outFile.getAbsolutePath() );
+            getLog().info( "Generating a pure DOAP file " + 
outputFile.getAbsolutePath() );
         }
 
-        XMLWriter writer = new PrettyPrintXMLWriter( w, 
givenProject.getModel().getModelEncoding(), null );
+        XMLWriter writer = new PrettyPrintXMLWriter( w, 
project.getModel().getModelEncoding(), null );
 
         // 
----------------------------------------------------------------------------
         // Convert POM to DOAP
@@ -473,6 +518,11 @@ public class DoapMojo
         writer.startElement( "Project" );
         if ( StringUtils.isNotEmpty( about ) )
         {
+            if ( artifact != null )
+            {
+                about = project.getUrl();
+            }
+
             try
             {
                 new URL( about );
@@ -490,10 +540,10 @@ public class DoapMojo
         }
 
         // name
-        writeName( writer, givenProject );
+        writeName( writer, project );
 
         // description
-        writeDescription( writer, givenProject );
+        writeDescription( writer, project );
 
         // implements
         writeImplements( writer );
@@ -502,28 +552,28 @@ public class DoapMojo
         writeAudience( writer );
 
         // Vendor
-        writeVendor( writer, givenProject );
+        writeVendor( writer, project );
 
         // created
-        writeCreated( writer, givenProject );
+        writeCreated( writer, project );
 
         // homepage and old-homepage
-        writeHomepage( writer, givenProject );
+        writeHomepage( writer, project );
 
         // Blog
         writeBlog( writer );
 
         // licenses
-        writeLicenses( writer, givenProject );
+        writeLicenses( writer, project );
 
         // programming-language
-        writeProgrammingLanguage( writer, givenProject );
+        writeProgrammingLanguage( writer, project );
 
         // category
-        writeCategory( writer, givenProject );
+        writeCategory( writer, project );
 
         // os
-        writeOS( writer, givenProject );
+        writeOS( writer, project );
 
         // Plateform
         writePlateform( writer );
@@ -532,39 +582,43 @@ public class DoapMojo
         writeLanguage( writer );
 
         // SCM
-        writeSourceRepositories( writer, givenProject );
+        writeSourceRepositories( writer, project );
 
         // bug-database
-        writeBugDatabase( writer, givenProject );
+        writeBugDatabase( writer, project );
 
         // mailing list
-        writeMailingList( writer, givenProject );
+        writeMailingList( writer, project );
 
         // download-page and download-mirror
-        writeDownloadPage( writer, givenProject );
+        writeDownloadPage( writer, project );
 
         // screenshots
-        writeScreenshots( writer, givenProject );
+        writeScreenshots( writer, project );
 
         // service-endpoint
         writeServiceEndpoint( writer );
 
         // wiki
-        writeWiki( writer, givenProject );
+        writeWiki( writer, project );
 
         // Releases
-        writeReleases( writer, givenProject );
+        writeReleases( writer, project );
 
         // Developers
-        writeContributors( writer, givenProject.getDevelopers() );
+        @SuppressWarnings( "unchecked" )
+        List<Contributor> developers = project.getDevelopers();
+        writeContributors( writer, developers );
 
         // Contributors
-        writeContributors( writer, givenProject.getContributors() );
+        @SuppressWarnings( "unchecked" )
+        List<Contributor> contributors = project.getContributors();
+        writeContributors( writer, contributors );
 
         // ASFext
         if ( asfExtOptions.isIncluded() )
         {
-            writeASFext( writer, givenProject );
+            writeASFext( writer, project );
         }
 
         writer.endElement(); // Project
@@ -584,7 +638,7 @@ public class DoapMojo
 
         if ( validate )
         {
-            List<String> errors = DoapUtil.validate( outFile );
+            List<String> errors = DoapUtil.validate( outputFile );
             if ( !errors.isEmpty() )
             {
                 for ( String error : errors )
@@ -597,10 +651,6 @@ public class DoapMojo
         }
     }
 
-    // ----------------------------------------------------------------------
-    // Private methods
-    // ----------------------------------------------------------------------
-
     /**
      * Write DOAP name.
      *

Modified: maven/plugins/trunk/maven-doap-plugin/src/site/apt/usage.apt.vm
URL: 
http://svn.apache.org/viewvc/maven/plugins/trunk/maven-doap-plugin/src/site/apt/usage.apt.vm?rev=1050381&r1=1050380&r2=1050381&view=diff
==============================================================================
--- maven/plugins/trunk/maven-doap-plugin/src/site/apt/usage.apt.vm (original)
+++ maven/plugins/trunk/maven-doap-plugin/src/site/apt/usage.apt.vm Fri Dec 17 
13:44:20 2010
@@ -60,12 +60,6 @@ Usage
 </project>
 +-----+
 
- To generate the DOAP file you would use the following command:
-
-+-----
-mvn doap:generate
-+-----
-
 * DOAP ASF Configuration
 
 +-----+
@@ -99,6 +93,38 @@ mvn doap:generate
 </project>
 +-----+
 
+* Advanced DOAP Configuration
+
++-----+
+<project>
+  <build>
+    <plugins>
+      ...
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-doap-plugin</artifactId>
+        <version>${project.version}</version>
+        <configuration>
+          <!-- generate DOAP file for a given artifact -->
+          <artifact>
+            <groupId>given-artifact-groupId</groupId>
+            <artifactId>given-artifact-artifactId</artifactId>
+            <version>given-artifact-version</version>
+          </artifact>
+
+          <!-- To not validate the generate DOAP file -->
+          <validate>false</validate>
+
+          ...
+        </configuration>
+      </plugin>
+      ...
+    </plugins>
+  </build>
+</project>
++-----+
+
+
 * Generate DOAP File
 
  To generate the DOAP file you would use the following command:

Modified: 
maven/plugins/trunk/maven-doap-plugin/src/test/java/org/apache/maven/plugin/doap/DoapMojoTest.java
URL: 
http://svn.apache.org/viewvc/maven/plugins/trunk/maven-doap-plugin/src/test/java/org/apache/maven/plugin/doap/DoapMojoTest.java?rev=1050381&r1=1050380&r2=1050381&view=diff
==============================================================================
--- 
maven/plugins/trunk/maven-doap-plugin/src/test/java/org/apache/maven/plugin/doap/DoapMojoTest.java
 (original)
+++ 
maven/plugins/trunk/maven-doap-plugin/src/test/java/org/apache/maven/plugin/doap/DoapMojoTest.java
 Fri Dec 17 13:44:20 2010
@@ -28,6 +28,7 @@ import java.util.List;
 import org.apache.maven.artifact.repository.ArtifactRepository;
 import org.apache.maven.artifact.repository.DefaultArtifactRepository;
 import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout;
+import org.apache.maven.plugin.doap.options.DoapArtifact;
 import org.apache.maven.plugin.testing.AbstractMojoTestCase;
 import org.apache.maven.project.MavenProject;
 import org.codehaus.plexus.util.IOUtil;
@@ -111,6 +112,57 @@ public class DoapMojoTest
     }
 
     /**
+     * Verify the generation of a DOAP file from an artifact.
+     *
+     * @throws Exception if any
+     */
+    public void testGeneratedDoapArtifact()
+        throws Exception
+    {
+        File pluginXmlFile =
+            new File( getBasedir(), 
"src/test/resources/unit/doap-configuration/doap-configuration-plugin-config.xml"
 );
+        DoapMojo mojo = (DoapMojo) lookupMojo( "generate", pluginXmlFile );
+        assertNotNull( "Mojo found.", mojo );
+
+        MavenProject mavenProject = (MavenProject) getVariableValueFromObject( 
mojo, "project" );
+        assertNotNull( mavenProject );
+
+        // Set some Mojo parameters
+        setVariableValueToObject( mojo, "remoteRepositories", 
getRemoteRepositories() );
+        setVariableValueToObject( mojo, "about", mavenProject.getUrl() );
+        DoapArtifact artifact = new DoapArtifact();
+        artifact.setGroupId( "org.apache.maven" );
+        artifact.setArtifactId( "maven-parent" );
+        artifact.setVersion( "18" );
+        setVariableValueToObject( mojo, "artifact", artifact );
+        setVariableValueToObject( mojo, "outputDirectory", 
"target/test/unit/doap-configuration/" );
+
+        mojo.execute();
+
+        File doapFile = new File( getBasedir(), 
"target/test/unit/doap-configuration/doap_maven-parent.rdf" );
+        assertTrue( "Doap File was not generated!", doapFile.exists() );
+
+        String readed = readFile( doapFile );
+
+        // Validate
+
+        // Pure DOAP
+        assertTrue( readed.indexOf( "<rdf:RDF xml:lang=\"en\" 
xmlns=\"http://usefulinc.com/ns/doap#\"; "
+            + "xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"; "
+            + "xmlns:foaf=\"http://xmlns.com/foaf/0.1/\"; " + 
"xmlns:asfext=\"http://projects.apache.org/ns/asfext#\";>" ) != -1 );
+        if ( StringUtils.isNotEmpty( mavenProject.getUrl() ) )
+        {
+            assertTrue( readed.indexOf( "<Project 
rdf:about=\"http://maven.apache.org/\";>" ) != -1 );
+            assertTrue( readed.indexOf( "<homepage 
rdf:resource=\"http://maven.apache.org/\"/>" ) != -1 );
+        }
+        assertTrue( readed.indexOf( "<name>Apache Maven</name>" ) != -1 );
+
+        // ASF ext
+        assertTrue( readed.indexOf( "<asfext:pmc 
rdf:resource=\"http://maven.apache.org/\"/>" ) != -1 );
+        assertTrue( readed.indexOf( "<asfext:name>Apache Maven</asfext:name>" 
) != -1 );
+    }
+
+    /**
      * Verify the generation of a DOAP file with ASF extension.
      *
      * @throws Exception if any
@@ -140,7 +192,7 @@ public class DoapMojoTest
 
         // Validate
 
-        // Pure DOAP
+        // ASF DOAP
         assertTrue( readed.indexOf( "<rdf:RDF xml:lang=\"en\" 
xmlns=\"http://usefulinc.com/ns/doap#\"; "
             + "xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"; "
             + "xmlns:foaf=\"http://xmlns.com/foaf/0.1/\"; " + 
"xmlns:asfext=\"http://projects.apache.org/ns/asfext#\";>" ) != -1 );


Reply via email to