Author: snicoll
Date: Sat Jan 21 07:55:44 2006
New Revision: 371078

URL: http://svn.apache.org/viewcvs?rev=371078&view=rev
Log:
- Changed the package structure to org.apache.maven.plugin.source
- Added the source:download goal

Added:
    maven/maven-1/plugins-sandbox/trunk/source/src/main/org/apache/maven/plugin/
    
maven/maven-1/plugins-sandbox/trunk/source/src/main/org/apache/maven/plugin/source/
      - copied from r370552, 
maven/maven-1/plugins-sandbox/trunk/source/src/main/org/apache/maven/source/
    
maven/maven-1/plugins-sandbox/trunk/source/src/main/org/apache/maven/plugin/source/JavaSourcesDownloader.java
   (with props)
Removed:
    maven/maven-1/plugins-sandbox/trunk/source/src/main/org/apache/maven/source/
Modified:
    maven/maven-1/plugins-sandbox/trunk/source/plugin.jelly
    maven/maven-1/plugins-sandbox/trunk/source/project.xml
    
maven/maven-1/plugins-sandbox/trunk/source/src/main/org/apache/maven/plugin/source/JavaSourceArtifactTypeHandler.java
    
maven/maven-1/plugins-sandbox/trunk/source/src/main/org/apache/maven/plugin/source/package.html
    maven/maven-1/plugins-sandbox/trunk/source/xdocs/goals.xml
    maven/maven-1/plugins-sandbox/trunk/source/xdocs/index.xml

Modified: maven/maven-1/plugins-sandbox/trunk/source/plugin.jelly
URL: 
http://svn.apache.org/viewcvs/maven/maven-1/plugins-sandbox/trunk/source/plugin.jelly?rev=371078&r1=371077&r2=371078&view=diff
==============================================================================
--- maven/maven-1/plugins-sandbox/trunk/source/plugin.jelly (original)
+++ maven/maven-1/plugins-sandbox/trunk/source/plugin.jelly Sat Jan 21 07:55:44 
2006
@@ -22,9 +22,20 @@
   xmlns:j="jelly:core"
   xmlns:ant="jelly:ant"
   xmlns:artifact="artifact"
-  xmlns:util="jelly:util">
+  xmlns:util="jelly:util"
+  xmlns:i="jelly:interaction"
+  xmlns:define="jelly:define"
+  xmlns:maven="jelly:maven"
+  xmlns:source="source">
+
+  <define:taglib uri="source">
+      <define:jellybean
+        name="download-java-sources"
+        className="org.apache.maven.plugin.source.JavaSourcesDownloader"
+        method="downloadJavaSources"/>
+  </define:taglib>
 
-  <j:new var="javaSourceTypeHandler" 
className="org.apache.maven.source.JavaSourceArtifactTypeHandler" />
+  <j:new var="javaSourceTypeHandler" 
className="org.apache.maven.plugin.source.JavaSourceArtifactTypeHandler" />
 
   <!-- ================================================================== -->
   <!-- S O U R C E                                                        -->
@@ -85,6 +96,48 @@
       type="java-source"
       project="${pom}"
       typeHandler="${javaSourceTypeHandler}"/>
+  </goal>
+
+
+  <!-- ================================================================== -->
+  <!-- D O W N L O A D   S O U R C E                                      -->
+  <!-- ================================================================== -->
+
+  <goal name="source:download" description="Download a source archive from the 
repository">
+    <j:if test="${empty(artifactId)}">
+      <i:ask question="What is the artifactId of the source archive to 
download?"
+             answer="artifactId"/>
+    </j:if>
+    <maven:param-check value="${artifactId}" fail="true" message="'artifactId' 
must be specified"/>
+
+    <j:if test="${empty(groupId)}">
+      <i:ask question="What is the groupId of the source archive to download?"
+             answer="groupId"/>
+    </j:if>
+    <maven:param-check value="${groupId}" fail="true" message="'groupId' must 
be specified"/>
+
+    <j:if test="${empty(version)}">
+      <i:ask question="What is the version of the source archive to download?"
+             answer="version"/>
+    </j:if>
+    <maven:param-check value="${version}" fail="true" message="'version' must 
be specified"/>
+
+    <j:if test="${empty(ignoreErrors)}">
+      <j:set var="ignoreErrors" value="false"/>
+    </j:if>
+
+    <j:if test="${empty(backwardCompatible)}">
+      <j:set var="backwardCompatible" value="false"/>
+    </j:if>
+
+    <source:download-java-sources
+          project="${pom}"
+          groupId="${groupId}"
+          artifactId="${artifactId}"
+          version="${version}"
+          ignoreErrors="${ignoreErrors}"
+          backwardCompatible="${backwardCompatible}"
+        />
   </goal>
 
 

Modified: maven/maven-1/plugins-sandbox/trunk/source/project.xml
URL: 
http://svn.apache.org/viewcvs/maven/maven-1/plugins-sandbox/trunk/source/project.xml?rev=371078&r1=371077&r2=371078&view=diff
==============================================================================
--- maven/maven-1/plugins-sandbox/trunk/source/project.xml (original)
+++ maven/maven-1/plugins-sandbox/trunk/source/project.xml Sat Jan 21 07:55:44 
2006
@@ -51,5 +51,43 @@
       <artifactId>maven</artifactId>
       <version>1.0.2</version>
     </dependency>
+    <dependency>
+      <groupId>maven</groupId>
+      <artifactId>maven-model</artifactId>
+      <version>3.0.1</version>
+      <properties>
+        <comment>This library is already loaded by maven's core. Be careful to 
use the same version number as in the core.</comment>
+      </properties>
+    </dependency>
+    <dependency>
+      <groupId>commons-logging</groupId>
+      <artifactId>commons-logging</artifactId>
+      <version>1.0.4</version>
+      <properties>
+        <comment>This library is already loaded by maven's core. Be careful to 
use the same version number as in the core.</comment>
+      </properties>
+    </dependency>
+    <dependency>
+      <groupId>commons-lang</groupId>
+      <artifactId>commons-lang</artifactId>
+      <version>2.0</version>
+      <type>jar</type>
+      <properties>
+        <comment>This library is already loaded by maven's core. Be careful to 
use the same version number as in the core.</comment>
+      </properties>
+    </dependency>
+    <dependency>
+      <groupId>commons-jelly</groupId>
+      <artifactId>commons-jelly</artifactId>
+      <version>1.0</version>
+      <properties>
+        <comment>This library is already loaded by maven's core. Be careful to 
use the same version number as in the core.</comment>
+      </properties>
+    </dependency>    
+    <dependency>
+      <groupId>commons-jelly</groupId>
+      <artifactId>commons-jelly-tags-interaction</artifactId>
+      <version>1.0</version>
+    </dependency>
   </dependencies>
 </project>

Modified: 
maven/maven-1/plugins-sandbox/trunk/source/src/main/org/apache/maven/plugin/source/JavaSourceArtifactTypeHandler.java
URL: 
http://svn.apache.org/viewcvs/maven/maven-1/plugins-sandbox/trunk/source/src/main/org/apache/maven/plugin/source/JavaSourceArtifactTypeHandler.java?rev=371078&r1=370552&r2=371078&view=diff
==============================================================================
--- 
maven/maven-1/plugins-sandbox/trunk/source/src/main/org/apache/maven/plugin/source/JavaSourceArtifactTypeHandler.java
 (original)
+++ 
maven/maven-1/plugins-sandbox/trunk/source/src/main/org/apache/maven/plugin/source/JavaSourceArtifactTypeHandler.java
 Sat Jan 21 07:55:44 2006
@@ -1,4 +1,4 @@
-package org.apache.maven.source;
+package org.apache.maven.plugin.source;
 
 /* ====================================================================
  *   Copyright 2001-2005 The Apache Software Foundation.

Added: 
maven/maven-1/plugins-sandbox/trunk/source/src/main/org/apache/maven/plugin/source/JavaSourcesDownloader.java
URL: 
http://svn.apache.org/viewcvs/maven/maven-1/plugins-sandbox/trunk/source/src/main/org/apache/maven/plugin/source/JavaSourcesDownloader.java?rev=371078&view=auto
==============================================================================
--- 
maven/maven-1/plugins-sandbox/trunk/source/src/main/org/apache/maven/plugin/source/JavaSourcesDownloader.java
 (added)
+++ 
maven/maven-1/plugins-sandbox/trunk/source/src/main/org/apache/maven/plugin/source/JavaSourcesDownloader.java
 Sat Jan 21 07:55:44 2006
@@ -0,0 +1,339 @@
+/*
+ * Copyright (c) 2006 Your Corporation. All Rights Reserved.
+ */
+package org.apache.maven.plugin.source;
+
+import org.apache.commons.lang.StringUtils;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.maven.AbstractMavenComponent;
+import org.apache.maven.project.Project;
+import org.apache.maven.util.HttpUtils;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.util.Iterator;
+
+/**
+ * An helper class used to download java sources archives.
+ *
+ * @author <a href="[EMAIL PROTECTED]">Stephane Nicoll</a>
+ * @version $Id$
+ */
+public class JavaSourcesDownloader
+    extends AbstractMavenComponent
+{
+    private static final String PROXY_LOGINHOST = "maven.proxy.ntlm.host";
+
+    private static final String PROXY_LOGINDOMAIN = "maven.proxy.ntlm.domain";
+
+    private static final Log log = LogFactory.getLog( 
JavaSourcesDownloader.class );
+
+    private Project project;
+
+    private String groupId;
+
+    private String artifactId;
+
+    private String version;
+
+    private boolean ignoreErrors;
+
+    private boolean backwardCompatible;
+
+
+    /**
+     * Download the Java sources archive from the configured repositories.
+     *
+     * @throws NullPointerException  if a mandatory parameter is not 
initialized
+     * @throws FileNotFoundException if ignoreErrors is false and the archive 
is not found
+     */
+    public void downloadJavaSources()
+        throws NullPointerException, FileNotFoundException
+    {
+        if ( project == null )
+        {
+            throw new NullPointerException( "project should be set." );
+        }
+
+        if ( groupId == null )
+        {
+            throw new NullPointerException( "groupId should be set." );
+        }
+
+        if ( artifactId == null )
+        {
+            throw new NullPointerException( "artifactId should be set." );
+        }
+
+        if ( version == null )
+        {
+            throw new NullPointerException( "version should be set." );
+        }
+
+        final String dependencyId = groupId + ":" + artifactId + ":" + version;
+
+        final String relativePath = buildRelativePath();
+        final File localFile = new File( 
project.getContext().getMavenRepoLocal(), relativePath );
+        final String backwareCompatibleRelativePath = 
buildBackwardCompatibleRelativePath();
+        final File backwardCompatibleLocalFile =
+            new File( project.getContext().getMavenRepoLocal(), 
backwareCompatibleRelativePath );
+
+        boolean artifactFound;
+        if ( isSnapshot() )
+        {
+            artifactFound = getRemoteArtifact( localFile, relativePath, 
dependencyId );
+            if ( backwardCompatible )
+            {
+                artifactFound =
+                    getRemoteArtifact( backwardCompatibleLocalFile, 
backwareCompatibleRelativePath, dependencyId );
+            }
+
+            // Do not go further if the artifact is in the local repository
+            if ( localFile.exists() || ( backwardCompatible && 
backwardCompatibleLocalFile.exists() ) )
+            {
+                return;
+            }
+        }
+        else
+        {
+            if ( localFile.exists() || ( backwardCompatible && 
backwardCompatibleLocalFile.exists() ) )
+            {
+                log.debug( "source for " + dependencyId + " is available in 
the local repository." );
+                return;
+            }
+            else
+            {
+                // download it
+                artifactFound = getRemoteArtifact( localFile, relativePath, 
dependencyId );
+                if ( !artifactFound && backwardCompatible )
+                {
+                    artifactFound =
+                        getRemoteArtifact( backwardCompatibleLocalFile, 
backwareCompatibleRelativePath, dependencyId );
+                }
+            }
+        }
+
+        if ( !ignoreErrors && !artifactFound )
+        {
+            throw new FileNotFoundException( "Could not download source for " 
+ dependencyId + " from any repository " +
+                getProject().getContext().getMavenRepoRemote() );
+        }
+    }
+
+    // Getters & Setters
+
+
+    public Project getProject()
+    {
+        return project;
+    }
+
+    public void setProject( final Project project )
+    {
+        this.project = project;
+    }
+
+    public String getGroupId()
+    {
+        return groupId;
+    }
+
+    public void setGroupId( final String groupId )
+    {
+        this.groupId = groupId;
+    }
+
+    public String getArtifactId()
+    {
+        return artifactId;
+    }
+
+    public void setArtifactId( final String artifactId )
+    {
+        this.artifactId = artifactId;
+    }
+
+    public String getVersion()
+    {
+        return version;
+    }
+
+    public void setVersion( final String version )
+    {
+        this.version = version;
+    }
+
+    public boolean isIgnoreErrors()
+    {
+        return ignoreErrors;
+    }
+
+    public void setIgnoreErrors( final boolean ignoreErrors )
+    {
+        this.ignoreErrors = ignoreErrors;
+    }
+
+    public boolean isBackwardCompatible()
+    {
+        return backwardCompatible;
+    }
+
+    public void setBackwardCompatible( final boolean backwardCompatible )
+    {
+        this.backwardCompatible = backwardCompatible;
+    }
+
+    /**
+     * Builds the relative path to the source archive.
+     *
+     * @return the relative path to the source archive
+     */
+    private String buildRelativePath()
+    {
+        StringBuffer sb = new StringBuffer();
+        sb.append( groupId ).append( "/java-sources/" ).append( artifactId 
).append( "-" ).append( version ).append(
+            "-sources.jar" );
+        return sb.toString();
+    }
+
+    /**
+     * Build the backware compatible relative path to the source archive.
+     *
+     * @return the backware compatible relative path to the source archive
+     */
+    private String buildBackwardCompatibleRelativePath()
+    {
+        StringBuffer sb = new StringBuffer();
+        sb.append( groupId ).append( "/src/" ).append( artifactId ).append( 
"-" ).append( version ).append( ".zip" );
+        return sb.toString();
+    }
+
+    /**
+     * Specify whether the source archive to download is a SNAPSHOT or not.
+     *
+     * @return true if the source archive is a SNAPSHOT, false otherwise
+     */
+    private boolean isSnapshot()
+    {
+        return version.endsWith( "SNAPSHOT" );
+    }
+
+    // Taken from maven core code in order to mimic the current behavior
+
+    /**
+     * Retrieve a <code>remoteFile</code> from the maven remote repositories
+     * and store it at <code>destinationFile</code>
+     *
+     * @param destinationFile the destination file in the local repository
+     * @param relativePath    the relative path to the dependency
+     * @param dependencyId    the dependency Id
+     * @return true if the retrieval succeeds, false otherwise.
+     */
+    private boolean getRemoteArtifact( File destinationFile, String 
relativePath, String dependencyId )
+    {
+
+        // The directory structure for the project this dependency belongs to
+        // may not exists so attempt to create the project directory structure
+        // before attempting to download the dependency.
+        File directory = destinationFile.getParentFile();
+
+        if ( !directory.exists() )
+        {
+            directory.mkdirs();
+        }
+
+        log.info( "Attempting to download sources for " + dependencyId + " 
("+relativePath+")" );
+
+        boolean artifactFound = false;
+
+        for ( Iterator i = 
getProject().getContext().getMavenRepoRemote().iterator(); i.hasNext(); )
+        {
+            String remoteRepo = (String) i.next();
+
+            if ( remoteRepo.endsWith( "/" ) )
+            {
+                remoteRepo = remoteRepo.substring( 0, remoteRepo.length() - 1 
);
+            }
+
+            // The username and password parameters are not being
+            // used here. Those are the "" parameters you see below.
+            String url = remoteRepo + "/" + relativePath;
+            url = StringUtils.replace( url, "//", "/" );
+
+            if ( !url.startsWith( "file" ) )
+            {
+                if ( url.startsWith( "https" ) )
+                {
+                    url = StringUtils.replace( url, "https:/", "https://"; );
+                }
+                else
+                {
+                    url = StringUtils.replace( url, "http:/", "http://"; );
+                }
+            }
+            log.debug( "Trying to download source at " + url );
+
+            // Attempt to retrieve the artifact and set the checksum if 
retrieval
+            // of the checksum file was successful.
+            try
+            {
+                String loginHost = (String) 
getProject().getContext().getVariable( PROXY_LOGINHOST );
+                String loginDomain = (String) 
getProject().getContext().getVariable( PROXY_LOGINDOMAIN );
+                HttpUtils.getFile( url, destinationFile, false, true, 
getProject().getContext().getProxyHost(),
+                                   getProject().getContext().getProxyPort(),
+                                   
getProject().getContext().getProxyUserName(),
+                                   
getProject().getContext().getProxyPassword(), loginHost, loginDomain, true );
+
+                // Artifact was found, continue checking additional remote 
repos (if any)
+                // in case there is a newer version (i.e. snapshots) in 
another repo
+                artifactFound = true;
+
+                if ( !isSnapshot() )
+                {
+                    log.info( "Downloded source for " + dependencyId + " at " 
+ url );
+                    break;
+                }
+            }
+            catch ( FileNotFoundException e )
+            {
+                // Multiple repositories may exist, and if the file is not 
found
+                // in just one of them, it's no problem, and we don't want to
+                // even print out an error.
+                // if it's not found at all, artifactFound will be false, and 
the
+                // build _will_ break, and the user will get an error message
+                log.debug( "File not found on one of the repos", e );
+            }
+            catch ( Exception e )
+            {
+                // If there are additional remote repos, then ignore exception
+                // as artifact may be found in another remote repo. If there
+                // are no more remote repos to check and the artifact wasn't 
found in
+                // a previous remote repo, then artifactFound is false 
indicating
+                // that the artifact could not be found in any of the remote 
repos
+                //
+                // arguably, we need to give the user better control (another 
command-
+                // line switch perhaps) of what to do in this case? Maven 
already has
+                // a command-line switch to work in offline mode, but what 
about when
+                // one of two or more remote repos is unavailable? There may 
be multiple
+                // remote repos for redundancy, in which case you probably 
want the build
+                // to continue. There may however be multiple remote repos 
because some
+                // artifacts are on one, and some are on another. In this 
case, you may
+                // want the build to break.
+                //
+                // print a warning, in any case, so user catches on to mistyped
+                // hostnames, or other snafus
+                // FIXME: localize this message
+                String[] parsedUrl = HttpUtils.parseUrl( url );
+                log.warn( "Error retrieving artifact from [" + parsedUrl[2] + 
"]: " + e );
+                if ( parsedUrl[0] != null )
+                {
+                    log.debug( "Username was '" + parsedUrl[0] + "', password 
hidden" );
+                }
+                log.debug( "Error details", e );
+            }
+        }
+
+        return artifactFound;
+    }
+}

Propchange: 
maven/maven-1/plugins-sandbox/trunk/source/src/main/org/apache/maven/plugin/source/JavaSourcesDownloader.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
maven/maven-1/plugins-sandbox/trunk/source/src/main/org/apache/maven/plugin/source/JavaSourcesDownloader.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Modified: 
maven/maven-1/plugins-sandbox/trunk/source/src/main/org/apache/maven/plugin/source/package.html
URL: 
http://svn.apache.org/viewcvs/maven/maven-1/plugins-sandbox/trunk/source/src/main/org/apache/maven/plugin/source/package.html?rev=371078&r1=370552&r2=371078&view=diff
==============================================================================
--- 
maven/maven-1/plugins-sandbox/trunk/source/src/main/org/apache/maven/plugin/source/package.html
 (original)
+++ 
maven/maven-1/plugins-sandbox/trunk/source/src/main/org/apache/maven/plugin/source/package.html
 Sat Jan 21 07:55:44 2006
@@ -17,7 +17,7 @@
  -->
 <html>
 <head>
-  <title>org.apache.maven.source</title>
+  <title>org.apache.maven.plugin.source</title>
 </head>
 <body>
 

Modified: maven/maven-1/plugins-sandbox/trunk/source/xdocs/goals.xml
URL: 
http://svn.apache.org/viewcvs/maven/maven-1/plugins-sandbox/trunk/source/xdocs/goals.xml?rev=371078&r1=371077&r2=371078&view=diff
==============================================================================
--- maven/maven-1/plugins-sandbox/trunk/source/xdocs/goals.xml (original)
+++ maven/maven-1/plugins-sandbox/trunk/source/xdocs/goals.xml Sat Jan 21 
07:55:44 2006
@@ -22,6 +22,10 @@
         <name>source:source</name>
         <description>Create the source archive.</description>
       </goal>
+      <goal>
+        <name>source:download</name>
+        <description>Download a source archive from the 
repository.</description>
+      </goal>
     </goals>
   </body>
 </document>

Modified: maven/maven-1/plugins-sandbox/trunk/source/xdocs/index.xml
URL: 
http://svn.apache.org/viewcvs/maven/maven-1/plugins-sandbox/trunk/source/xdocs/index.xml?rev=371078&r1=371077&r2=371078&view=diff
==============================================================================
--- maven/maven-1/plugins-sandbox/trunk/source/xdocs/index.xml (original)
+++ maven/maven-1/plugins-sandbox/trunk/source/xdocs/index.xml Sat Jan 21 
07:55:44 2006
@@ -32,6 +32,10 @@
         in this property is used to generate the source archive.
       </p>
       <p>
+        The generated source archive will be deployed at
+        
<code>${groupId}/java-sources/${artifactId}-{version}-sources.jar</code>.
+      </p>
+      <p>
         For more information on the functionality provided by this plugin,
         please see the <a href="goals.html">Goals</a> document.
       </p>
@@ -40,6 +44,30 @@
         by this plugin, please see the <a href="properties.html">properties</a>
         document.
       </p>
+    </section>
+    <section name="Downloading a source archive">
+      <p>
+        Similarly to the plugin plugin, the source plugin is able to download
+        a source archive from the configured repository(ies). The following
+        parameters might be set at runtime:
+        <ul>
+          <li>groupId: the groupId of the source archive to download</li>
+          <li>artifactId: the artifactId of the source archive to download</li>
+          <li>version: the version of the source archive to download</li>
+          <li>ignoreErrors (optional - default to false): specificy if the 
build should fail if the source archive is not found</li>
+          <li>backwardCompatible (optional - default to false): specificy if 
the old source archive location should be checked as well</li>
+        </ul>
+      </p>
+      <p>
+        Example:
+      </p>
+      <source>
+          maven source:download
+                  -DartifactId=someArtifact
+                  -DgroupId=theGroup
+                  -Dversion=1.0.0-SNAPSHOT
+                  -DbackwardCompatible=true
+      </source>
     </section>
  </body>
 </document>


Reply via email to