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.util.List;
24  
25  import org.apache.maven.artifact.Artifact;
26  import org.apache.maven.plugin.MojoExecutionException;
27  import org.apache.maven.plugin.MojoFailureException;
28  import org.apache.maven.plugins.annotations.Parameter;
29  import org.apache.tools.ant.Project;
30  
31  import com.github.maven_nar.cpptasks.CUtil;
32  import com.github.maven_nar.cpptasks.LinkerDef;
33  import com.github.maven_nar.cpptasks.types.LibrarySet;
34  import com.github.maven_nar.cpptasks.types.LibraryTypeEnum;
35  
36  /**
37   * Keeps info on a library
38   *
39   * @author Mark Donszelmann
40   */
41  public class Lib {
42  
43    /**
44     * Name of the library, or a dependency groupId:artifactId if this library
45     * contains sublibraries
46     */
47    @Parameter(required = true)
48    private String name;
49  
50    /**
51     * Type of linking for this library
52     */
53    @Parameter(defaultValue = "shared", required = true)
54    private String type = Library.SHARED;
55  
56    /**
57     * Location for this library
58     */
59    @Parameter(required = true)
60    private File directory;
61  
62    /**
63     * Sub libraries for this library
64     */
65    @Parameter
66    private List/* <Lib> */libs;
67  
68    public final void addLibSet(final AbstractDependencyMojo mojo, final LinkerDef linker, final Project antProject)
69        throws MojoFailureException, MojoExecutionException {
70      if (this.name == null) {
71        throw new MojoFailureException("NAR: Please specify <Name> as part of <Lib> for library \"" + this.name + "\"");
72      }
73      addLibSet(mojo, linker, antProject, this.name, this.directory);
74    }
75  
76    private void addLibSet(final AbstractDependencyMojo mojo, final LinkerDef linker, final Project antProject,
77        final String name, final File dir) throws MojoFailureException, MojoExecutionException {
78      if (this.libs == null) {
79        addSingleLibSet(linker, antProject, name, dir);
80      } else {
81        addMultipleLibSets(mojo, linker, antProject, name);
82      }
83    }
84  
85    private void addMultipleLibSets(final AbstractDependencyMojo mojo, final LinkerDef linker, final Project antProject,
86        final String name) throws MojoFailureException, MojoExecutionException {
87      final List dependencies = mojo.getNarArtifacts();
88      for (final Object lib1 : this.libs) {
89        final Lib lib = (Lib) lib1;
90        final String[] ids = name.split(":", 2);
91        if (ids.length != 2) {
92          throw new MojoFailureException("NAR: Please specify <Name> as part of <Lib> in format 'groupId:artifactId'");
93        }
94        for (final Object dependency1 : dependencies) {
95          final Artifact dependency = (Artifact) dependency1;
96          if (dependency.getGroupId().equals(ids[0]) && dependency.getArtifactId().equals(ids[1])) {
97            // FIXME NAR-90
98            final File narDir = new File(dependency.getFile().getParentFile(),
99                "nar/lib/" + mojo.getAOL() + "/" + lib.type);
100           final String narName = dependency.getArtifactId() + "-" + lib.name + "-" + dependency.getBaseVersion();
101           lib.addLibSet(mojo, linker, antProject, narName, narDir);
102         }
103       }
104     }
105   }
106 
107   private void addSingleLibSet(final LinkerDef linker, final Project antProject, final String name, final File dir)
108       throws MojoFailureException, MojoExecutionException {
109     if (!this.type.equals("framework") && dir == null) {
110       throw new MojoFailureException("NAR: Please specify <Directory> as part of <Lib> for library \"" + name + "\"");
111     }
112     final LibrarySet libSet = new LibrarySet();
113     libSet.setProject(antProject);
114     libSet.setLibs(new CUtil.StringArrayBuilder(name));
115     final LibraryTypeEnum libType = new LibraryTypeEnum();
116     libType.setValue(this.type);
117     libSet.setType(libType);
118     libSet.setDir(dir);
119     linker.addLibset(libSet);
120   }
121 }