You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by vs...@apache.org on 2008/01/24 00:59:04 UTC
svn commit: r614740 - in /maven/shared/trunk/maven-doxia-tools: pom.xml
src/main/java/org/apache/maven/doxia/tools/DefaultSiteTool.java
src/main/java/org/apache/maven/doxia/tools/SiteTool.java
src/test/java/org/apache/maven/doxia/tools/SiteToolTest.java
Author: vsiveton
Date: Wed Jan 23 15:58:57 2008
New Revision: 614740
URL: http://svn.apache.org/viewvc?rev=614740&view=rev
Log:
MSITE-290: Move logic from AbstractSiteMojo and AbstractSiteRenderingMojo for Doxia related stuff
o moved from o.a.m.p.site:
- AbstractSiteMojo#getSiteDescriptorFile( File basedir, Locale locale )
- AbstractSiteRenderingMojo#getSiteDescriptorFromRepository( MavenProject project, Locale locale ) with a small fix for mkdirs()
o added more test cases
Modified:
maven/shared/trunk/maven-doxia-tools/pom.xml
maven/shared/trunk/maven-doxia-tools/src/main/java/org/apache/maven/doxia/tools/DefaultSiteTool.java
maven/shared/trunk/maven-doxia-tools/src/main/java/org/apache/maven/doxia/tools/SiteTool.java
maven/shared/trunk/maven-doxia-tools/src/test/java/org/apache/maven/doxia/tools/SiteToolTest.java
Modified: maven/shared/trunk/maven-doxia-tools/pom.xml
URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-doxia-tools/pom.xml?rev=614740&r1=614739&r2=614740&view=diff
==============================================================================
--- maven/shared/trunk/maven-doxia-tools/pom.xml (original)
+++ maven/shared/trunk/maven-doxia-tools/pom.xml Wed Jan 23 15:58:57 2008
@@ -59,6 +59,11 @@
<!-- maven -->
<dependency>
<groupId>org.apache.maven</groupId>
+ <artifactId>maven-project</artifactId>
+ <version>2.0</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.maven</groupId>
<artifactId>maven-artifact</artifactId>
<version>2.0.2</version>
</dependency>
Modified: maven/shared/trunk/maven-doxia-tools/src/main/java/org/apache/maven/doxia/tools/DefaultSiteTool.java
URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-doxia-tools/src/main/java/org/apache/maven/doxia/tools/DefaultSiteTool.java?rev=614740&r1=614739&r2=614740&view=diff
==============================================================================
--- maven/shared/trunk/maven-doxia-tools/src/main/java/org/apache/maven/doxia/tools/DefaultSiteTool.java (original)
+++ maven/shared/trunk/maven-doxia-tools/src/main/java/org/apache/maven/doxia/tools/DefaultSiteTool.java Wed Jan 23 15:58:57 2008
@@ -20,9 +20,11 @@
*/
import java.io.File;
+import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
+import java.util.Locale;
import java.util.StringTokenizer;
import org.apache.maven.artifact.Artifact;
@@ -35,6 +37,7 @@
import org.apache.maven.artifact.versioning.VersionRange;
import org.apache.maven.doxia.site.decoration.DecorationModel;
import org.apache.maven.doxia.site.decoration.Skin;
+import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.logging.AbstractLogEnabled;
/**
@@ -337,5 +340,148 @@
}
return relativePath;
+ }
+
+ /** {@inheritDoc} */
+ public File getSiteDescriptorFromBasedir( File siteDirectory, File basedir, Locale locale )
+ {
+ if ( basedir == null )
+ {
+ throw new IllegalArgumentException( "basedir could not be null" );
+ }
+
+ if ( siteDirectory == null )
+ {
+ siteDirectory = new File( basedir, "src/site" );
+ }
+ if ( locale == null )
+ {
+ locale = new Locale( "" );
+ }
+
+ String relativePath = getRelativePath( siteDirectory.getAbsolutePath(), basedir.getAbsolutePath() );
+
+ File siteDescriptor = new File( relativePath, "site_" + locale.getLanguage() + ".xml" );
+
+ if ( !siteDescriptor.exists() )
+ {
+ siteDescriptor = new File( relativePath, "site.xml" );
+ }
+ return siteDescriptor;
+ }
+
+ /** {@inheritDoc} */
+ public File getSiteDescriptorFromRepository( MavenProject project, ArtifactRepository localRepository, List remoteArtifactRepositories, Locale locale )
+ throws SiteToolException
+ {
+ if ( project == null )
+ {
+ throw new IllegalArgumentException( "project could not be null" );
+ }
+ if ( localRepository == null )
+ {
+ throw new IllegalArgumentException( "localRepository could not be null" );
+ }
+ if ( remoteArtifactRepositories == null )
+ {
+ throw new IllegalArgumentException( "remoteArtifactRepositories could not be null" );
+ }
+
+ if ( locale == null )
+ {
+ locale = new Locale( "" );
+ }
+
+ try
+ {
+ return resolveSiteDescriptor( project, localRepository, remoteArtifactRepositories, locale );
+ }
+ catch ( ArtifactNotFoundException e )
+ {
+ getLogger().debug( "ArtifactNotFoundException: Unable to locate site descriptor: " + e );
+ return null;
+ }
+ catch ( ArtifactResolutionException e )
+ {
+ throw new SiteToolException( "ArtifactResolutionException: Unable to locate site descriptor: " + e.getMessage() );
+ }
+ catch ( IOException e )
+ {
+ throw new SiteToolException( "IOException: Unable to locate site descriptor: " + e.getMessage() );
+ }
+ }
+
+ // ----------------------------------------------------------------------
+ // Private methods
+ // ----------------------------------------------------------------------
+
+ private File resolveSiteDescriptor( MavenProject project, ArtifactRepository localRepository, List repositories, Locale locale )
+ throws IOException, ArtifactResolutionException, ArtifactNotFoundException
+ {
+ File result;
+
+ // TODO: this is a bit crude - proper type, or proper handling as metadata rather than an artifact in 2.1?
+ Artifact artifact = artifactFactory.createArtifactWithClassifier( project.getGroupId(),
+ project.getArtifactId(),
+ project.getVersion(), "xml", "site_"
+ + locale.getLanguage() );
+
+ boolean found = false;
+ try
+ {
+ artifactResolver.resolve( artifact, repositories, localRepository );
+
+ result = artifact.getFile();
+
+ // we use zero length files to avoid re-resolution (see below)
+ if ( result.length() > 0 )
+ {
+ found = true;
+ }
+ else
+ {
+ getLogger().debug( "Skipped locale's site descriptor" );
+ }
+ }
+ catch ( ArtifactNotFoundException e )
+ {
+ getLogger().debug( "Unable to locate locale's site descriptor: " + e );
+
+ // we can afford to write an empty descriptor here as we don't expect it to turn up later in the remote
+ // repository, because the parent was already released (and snapshots are updated automatically if changed)
+ result = new File( localRepository.getBasedir(), localRepository.pathOf( artifact ) );
+ result.getParentFile().mkdirs();
+ result.createNewFile();
+ }
+
+ if ( !found )
+ {
+ artifact = artifactFactory.createArtifactWithClassifier( project.getGroupId(), project.getArtifactId(),
+ project.getVersion(), "xml", "site" );
+ try
+ {
+ artifactResolver.resolve( artifact, repositories, localRepository );
+ }
+ catch ( ArtifactNotFoundException e )
+ {
+ // see above regarding this zero length file
+ result = new File( localRepository.getBasedir(), localRepository.pathOf( artifact ) );
+ result.getParentFile().mkdirs();
+ result.createNewFile();
+
+ throw e;
+ }
+
+ result = artifact.getFile();
+
+ // we use zero length files to avoid re-resolution (see below)
+ if ( result.length() == 0 )
+ {
+ getLogger().debug( "Skipped remote site descriptor check" );
+ result = null;
+ }
+ }
+
+ return result;
}
}
Modified: maven/shared/trunk/maven-doxia-tools/src/main/java/org/apache/maven/doxia/tools/SiteTool.java
URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-doxia-tools/src/main/java/org/apache/maven/doxia/tools/SiteTool.java?rev=614740&r1=614739&r2=614740&view=diff
==============================================================================
--- maven/shared/trunk/maven-doxia-tools/src/main/java/org/apache/maven/doxia/tools/SiteTool.java (original)
+++ maven/shared/trunk/maven-doxia-tools/src/main/java/org/apache/maven/doxia/tools/SiteTool.java Wed Jan 23 15:58:57 2008
@@ -19,12 +19,15 @@
* under the License.
*/
+import java.io.File;
import java.util.List;
+import java.util.Locale;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.doxia.site.decoration.DecorationModel;
import org.apache.maven.doxia.site.decoration.Skin;
+import org.apache.maven.project.MavenProject;
/**
* This tool contains some utilities methods to play with Doxia <code>DecorationModel</code>.
@@ -76,4 +79,24 @@
* @return a relative path from <code>from</code> to <code>to</code>.
*/
String getRelativePath( String to, String from );
+
+ /**
+ * @param siteDirectory containing the <code>site.xml</code> file. If null, using by default "${basedir}/src/site".
+ * @param basedir not null.
+ * @param locale the locale wanted for the site descriptor. If not null, searching for <code>site_<i>localeLanguage</i>.xml</code>
+ * @return the site descriptor relative file, i.e. <code>src/site/site.xml</code>, depending parameters value.
+ */
+ File getSiteDescriptorFromBasedir( File siteDirectory, File basedir, Locale locale );
+
+ /**
+ * @param project Maven project not null
+ * @param localRepository not null
+ * @param repositories not null
+ * @param locale the locale wanted for the site descriptor. If not null, searching for <code>site_<i>localeLanguage</i>.xml</code>
+ * @return the site descriptor into the local repository after download of it from repositories or null if not found in repositories.
+ * @throws SiteToolException if any
+ */
+ File getSiteDescriptorFromRepository( MavenProject project, ArtifactRepository localRepository,
+ List repositories, Locale locale )
+ throws SiteToolException;
}
Modified: maven/shared/trunk/maven-doxia-tools/src/test/java/org/apache/maven/doxia/tools/SiteToolTest.java
URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-doxia-tools/src/test/java/org/apache/maven/doxia/tools/SiteToolTest.java?rev=614740&r1=614739&r2=614740&view=diff
==============================================================================
--- maven/shared/trunk/maven-doxia-tools/src/test/java/org/apache/maven/doxia/tools/SiteToolTest.java (original)
+++ maven/shared/trunk/maven-doxia-tools/src/test/java/org/apache/maven/doxia/tools/SiteToolTest.java Wed Jan 23 15:58:57 2008
@@ -20,6 +20,7 @@
*/
import java.io.File;
+import java.util.Locale;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
@@ -74,6 +75,15 @@
/**
* @throws Exception
*/
+ protected File getLocalRepoDir()
+ throws Exception
+ {
+ return new File( getLocalRepo().getBasedir() );
+ }
+
+ /**
+ * @throws Exception
+ */
public void testGetDefaultSkinArtifact()
throws Exception
{
@@ -121,5 +131,46 @@
to = "http://maven.apache.org/plugins/maven-site-plugin/";
from = "http://maven.apache.org";
assertEquals( tool.getRelativePath( to, from ), "plugins" + File.separator + "maven-site-plugin" );
+ }
+
+ /**
+ * @throws Exception
+ */
+ public void testGetSiteDescriptorFromBasedir()
+ throws Exception
+ {
+ SiteTool tool = (SiteTool) lookup( SiteTool.ROLE );
+ assertNotNull( tool );
+
+ SiteToolMavenProjectStub project = new SiteToolMavenProjectStub();
+ assertEquals( tool.getSiteDescriptorFromBasedir( null, project.getBasedir(), null ).toString(), "src"
+ + File.separator + "site" + File.separator + "site.xml" );
+ assertEquals( tool.getSiteDescriptorFromBasedir( null, project.getBasedir(), Locale.ENGLISH ).toString(), "src"
+ + File.separator + "site" + File.separator + "site.xml" );
+ File siteDir = new File( project.getBasedir(), "src/blabla" );
+ assertEquals( tool.getSiteDescriptorFromBasedir( siteDir, project.getBasedir(), null ).toString(), "src"
+ + File.separator + "blabla" + File.separator + "site.xml" );
+ }
+
+ /**
+ * @throws Exception
+ */
+ public void testGetSiteDescriptorFromRepository()
+ throws Exception
+ {
+ SiteTool tool = (SiteTool) lookup( SiteTool.ROLE );
+ assertNotNull( tool );
+
+ SiteToolMavenProjectStub project = new SiteToolMavenProjectStub();
+ project.setGroupId( "org.apache.maven" );
+ project.setArtifactId( "maven-site" );
+ project.setVersion( "1.0" );
+ String result = getLocalRepoDir() + File.separator + "org" + File.separator + "apache" + File.separator
+ + "maven" + File.separator + "maven-site" + File.separator + "1.0" + File.separator
+ + "maven-site-1.0-site.xml";
+
+ assertEquals( tool.getSiteDescriptorFromRepository( project, getLocalRepo(),
+ project.getRemoteArtifactRepositories(), Locale.ENGLISH )
+ .toString(), result );
}
}