View Javadoc

1   /*
2    * #%L
3    * Native ARchive plugin for Maven
4    * %%
5    * Copyright (C) 2002 - 2014 NAR Maven Plugin developers.
6    * %%
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   * 
11   * http://www.apache.org/licenses/LICENSE-2.0
12   * 
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   * #L%
19   */
20  package com.github.maven_nar;
21  
22  import java.io.File;
23  import java.io.IOException;
24  import java.util.List;
25  
26  import org.apache.maven.artifact.Artifact;
27  import org.apache.maven.plugin.MojoExecutionException;
28  import org.apache.maven.plugin.MojoFailureException;
29  import org.apache.maven.plugins.annotations.LifecyclePhase;
30  import org.apache.maven.plugins.annotations.Mojo;
31  import org.apache.maven.plugins.annotations.ResolutionScope;
32  import org.apache.maven.shared.artifact.filter.collection.ScopeFilter;
33  import org.codehaus.plexus.util.FileUtils;
34  
35  /**
36   * Assemble libraries of NAR files.
37   *
38   * @author Mark Donszelmann
39   */
40  @Mojo(name = "nar-assembly", defaultPhase = LifecyclePhase.PROCESS_RESOURCES, requiresProject = true,
41    requiresDependencyResolution = ResolutionScope.TEST)
42  public class NarAssemblyMojo extends AbstractDependencyMojo {
43    /**
44     * List the dependencies we want to assemble
45     */
46    @Override
47    protected ScopeFilter getArtifactScopeFilter() {
48      // Was Artifact.SCOPE_RUNTIME  + provided?
49      // Think Provided isn't appropriate in Assembly - otherwise it isn't provided.
50      return new ScopeFilter( Artifact.SCOPE_RUNTIME, null );
51    }
52  
53    /**
54     * Copies the unpacked nar libraries and files into the projects target area
55     */
56    @Override
57    public final void narExecute() throws MojoExecutionException, MojoFailureException {
58      // download the dependencies if needed in local maven repository.
59      List<AttachedNarArtifact> attachedNarArtifacts = getAttachedNarArtifacts(libraries);
60      downloadAttachedNars(attachedNarArtifacts);
61  
62      // Warning, for SNAPSHOT artifacts that were not in the local maven
63      // repository, the downloadAttachedNars
64      // method above modified the version in the AttachedNarArtifact object to
65      // set the timestamp version from
66      // the web repository.
67      // In order to unpack the files with the correct names we need to get back
68      // the AttachedNarArtifact objects with
69      // -SNAPSHOT versions, so we call again getAttachedNarArtifacts() to get the
70      // unmodified AttachedNarArtifact
71      // objects
72      attachedNarArtifacts = getAttachedNarArtifacts(libraries);
73      unpackAttachedNars(attachedNarArtifacts);
74  
75      // this may make some extra copies...
76      for (final Object element : attachedNarArtifacts) {
77        final Artifact dependency = (Artifact) element;
78        getLog().debug("Assemble from " + dependency);
79  
80        // FIXME reported to maven developer list, isSnapshot
81        // changes behaviour
82        // of getBaseVersion, called in pathOf.
83        dependency.isSnapshot();
84  
85        final File srcDir = getLayout().getNarUnpackDirectory(getUnpackDirectory(),
86            getNarManager().getNarFile(dependency));
87        // File srcDir = new File( getLocalRepository().pathOf( dependency ) );
88        // srcDir = new File( getLocalRepository().getBasedir(),
89        // srcDir.getParent() );
90        // srcDir = new File( srcDir, "nar/" );
91  
92        final File dstDir = getTargetDirectory();
93        try {
94          FileUtils.mkdir(dstDir.getPath());
95          getLog().debug("SrcDir: " + srcDir);
96          if (srcDir.exists()) {
97            FileUtils.copyDirectoryStructureIfModified(srcDir, dstDir);
98          }
99        } catch (final IOException ioe) {
100         throw new MojoExecutionException("Failed to copy directory for dependency " + dependency + " from " + srcDir
101             + " to " + dstDir, ioe);
102       }
103     }
104   }
105 }