View Javadoc

1   package org.codehaus.mojo.javascript.archive;
2   
3   /*
4    * Derivative Work
5    * Copyright 2010 SOFTEC sa. All rights reserved.
6    *
7    * Original Work
8    * Copyright 2001-2005 The Apache Software Foundation.
9    *
10   * Licensed under the Apache License, Version 2.0 (the "License");
11   * you may not use this file except in compliance with the License.
12   * You may obtain a copy of the License at
13   *
14   *      http://www.apache.org/licenses/LICENSE-2.0
15   *
16   * Unless required by applicable law or agreed to in writing, software
17   * distributed under the License is distributed on an "AS IS" BASIS,
18   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19   * See the License for the specific language governing permissions and
20   * limitations under the License.
21   */
22  
23  import java.io.File;
24  import java.util.Iterator;
25  import java.util.Set;
26  
27  import org.apache.maven.artifact.Artifact;
28  import org.apache.maven.artifact.resolver.filter.ArtifactFilter;
29  import org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter;
30  import org.apache.maven.project.MavenProject;
31  import org.codehaus.plexus.archiver.ArchiverException;
32  import org.codehaus.plexus.archiver.UnArchiver;
33  import org.codehaus.plexus.logging.AbstractLogEnabled;
34  
35  /**
36   * A component to handle javascript dependencies.
37   * 
38   * @author <a href="mailto:nicolas.deloof@gmail.com">nicolas De Loof</a>
39   * @plexus.component role="org.codehaus.mojo.javascript.archive.JavascriptArtifactManager"
40   */
41  public class JavascriptArtifactManager
42      extends AbstractLogEnabled
43  {
44  
45      /**
46       * Plexus un-archiver.
47       * 
48       * @plexus.requirement role-hint="javascript"
49       */
50      private UnArchiver archiver;
51  
52      public JavascriptArtifactManager()
53      {
54          super();
55      }
56  
57      public int unpack( MavenProject project, String scope, File target, boolean useArtifactId )
58          throws ArchiverException
59      {
60          int count = 0;
61          archiver.setOverwrite( false );
62  
63          Set dependencies = project.getArtifacts();
64          ArtifactFilter runtime = new ScopeArtifactFilter( scope );
65          for ( Iterator iterator = dependencies.iterator(); iterator.hasNext(); )
66          {
67              Artifact dependency = (Artifact) iterator.next();
68              if ( !dependency.isOptional() && Types.JAVASCRIPT_TYPE.equals( dependency.getType() )
69                  && runtime.include( dependency ) )
70              {
71                  getLogger().info( "Unpack javascript dependency [" + dependency.toString() + "]" );
72                  archiver.setSourceFile( dependency.getFile() );
73  
74                  File dest = target;
75                  if ( useArtifactId )
76                  {
77                      dest = new File( target, dependency.getArtifactId() );
78                  }
79                  unpack( dependency, dest );
80                  count++;
81              }
82          }
83  
84          return count;
85      }
86  
87      /**
88       *
89       */
90      public void unpack( Artifact artifact, File target )
91          throws ArchiverException
92      {
93          archiver.setSourceFile( artifact.getFile() );
94          target.mkdirs();
95          archiver.setDestDirectory( target );
96          archiver.setOverwrite( false );
97          try
98          {
99              archiver.extract();
100         }
101         catch ( Exception e )
102         {
103             throw new ArchiverException( "Failed to extract javascript artifact to " + target, e );
104         }
105 
106     }
107 }