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  
25  import org.apache.maven.plugin.MojoExecutionException;
26  import org.apache.maven.plugin.MojoFailureException;
27  import org.apache.maven.plugin.logging.Log;
28  import org.apache.maven.project.MavenProject;
29  import org.apache.maven.project.MavenProjectHelper;
30  import org.codehaus.plexus.archiver.manager.ArchiverManager;
31  import org.codehaus.plexus.util.FileUtils;
32  
33  /**
34   * Initial layout which expands a nar file into:
35   *
36   * <pre>
37   * nar/includue
38   * nar/bin
39   * nar/lib
40   * </pre>
41   *
42   * this layout was abandoned because there is no one-to-one relation between the
43   * nar file and its directory structure.
44   * Therefore SNAPSHOTS could not be fully deleted when replaced.
45   *
46   * @author Mark Donszelmann (Mark.Donszelmann@gmail.com)
47   */
48  public class NarLayout20 extends AbstractNarLayout {
49    private final NarFileLayout fileLayout;
50  
51    public NarLayout20(final Log log) {
52      super(log);
53      this.fileLayout = new NarFileLayout10();
54    }
55  
56    /*
57     * (non-Javadoc)
58     * 
59     * @see com.github.maven_nar.NarLayout#attachNars(java.io.File,
60     * org.apache.maven.project.MavenProjectHelper,
61     * org.apache.maven.project.MavenProject, com.github.maven_nar.NarInfo)
62     */
63    @Override
64    public final void attachNars(final File baseDir, final ArchiverManager archiverManager,
65        final MavenProjectHelper projectHelper, final MavenProject project) throws MojoExecutionException {
66      if (getIncludeDirectory(baseDir, project.getArtifactId(), project.getVersion()).exists()) {
67        attachNar(archiverManager, projectHelper, project, "noarch", baseDir, "include/**");
68      }
69  
70      final String[] binAOL = new File(baseDir, "bin").list();
71      for (int i = 0; binAOL != null && i < binAOL.length; i++) {
72        attachNar(archiverManager, projectHelper, project, binAOL[i] + "-" + Library.EXECUTABLE, baseDir, "bin/"
73            + binAOL[i] + "/**");
74      }
75  
76      final File libDir = new File(baseDir, "lib");
77      final String[] libAOL = libDir.list();
78      for (int i = 0; libAOL != null && i < libAOL.length; i++) {
79        final String bindingType = null;
80        final String[] libType = new File(libDir, libAOL[i]).list();
81        for (int j = 0; libType != null && j < libType.length; j++) {
82          attachNar(archiverManager, projectHelper, project, libAOL[i] + "-" + libType[j], baseDir, "lib/" + libAOL[i]
83              + "/" + libType[j] + "/**");
84        }
85  
86      }
87    }
88  
89    /*
90     * (non-Javadoc)
91     * 
92     * @see com.github.maven_nar.NarLayout#getBinDirectory(java.io.File,
93     * java.lang.String)
94     */
95    @Override
96    public final File
97        getBinDirectory(final File baseDir, final String artifactId, final String version, final String aol) {
98      final File dir = new File(baseDir, this.fileLayout.getBinDirectory(aol));
99      return dir;
100   }
101 
102   /*
103    * (non-Javadoc)
104    * 
105    * @see com.github.maven_nar.NarLayout#getIncludeDirectory(java.io.File)
106    */
107   @Override
108   public final File getIncludeDirectory(final File baseDir, final String artifactId, final String version) {
109     return new File(baseDir, this.fileLayout.getIncludeDirectory());
110   }
111 
112   /*
113    * (non-Javadoc)
114    * 
115    * @see com.github.maven_nar.NarLayout#getLibDir(java.io.File,
116    * com.github.maven_nar.AOL, String type)
117    */
118   @Override
119   public final File getLibDirectory(final File baseDir, final String artifactId, final String version,
120       final String aol, final String type) throws MojoFailureException {
121     if (type.equals(Library.EXECUTABLE)) {
122       throw new MojoFailureException("INTERNAL ERROR, Replace call to getLibDirectory with getBinDirectory");
123     }
124 
125     final File dir = new File(baseDir, this.fileLayout.getLibDirectory(aol, type));
126     return dir;
127   }
128 
129   @Override
130   public File getNarUnpackDirectory(final File baseUnpackDirectory, final File narFile) {
131     final File dir = new File(baseUnpackDirectory, FileUtils.basename(narFile.getPath(), "."
132         + NarConstants.NAR_EXTENSION));
133     return dir;
134   }
135 
136   /*
137    * (non-Javadoc)
138    * 
139    * @see com.github.maven_nar.NarLayout#getNoArchDirectory(java.io.File)
140    */
141   @Override
142   public File getNoArchDirectory(final File baseDir, final String artifactId, final String version)
143       throws MojoExecutionException, MojoFailureException {
144     return baseDir;
145   }
146 
147   /*
148    * (non-Javadoc)
149    * 
150    * @see com.github.maven_nar.NarLayout#attachNars(java.io.File,
151    * org.apache.maven.project.MavenProjectHelper,
152    * org.apache.maven.project.MavenProject, com.github.maven_nar.NarInfo)
153    */
154   @Override
155   public final void prepareNarInfo(final File baseDir, final MavenProject project, final NarInfo narInfo,
156       final AbstractNarMojo mojo) throws MojoExecutionException {
157     if (getIncludeDirectory(baseDir, project.getArtifactId(), project.getVersion()).exists()) {
158       narInfo.setNar(null, "noarch", project.getGroupId() + ":" + project.getArtifactId() + ":" + NarConstants.NAR_TYPE
159           + ":" + "noarch");
160     }
161 
162     final String[] binAOL = new File(baseDir, "bin").list();
163     for (int i = 0; binAOL != null && i < binAOL.length; i++) {// TODO: chose
164                                                                // not to apply
165                                                                // new file
166                                                                // naming for
167                                                                // outfile in
168                                                                // case of
169                                                                // backwards
170                                                                // compatability,
171                                                                // may need to
172                                                                // reconsider
173       narInfo.setNar(null, Library.EXECUTABLE, project.getGroupId() + ":" + project.getArtifactId() + ":"
174           + NarConstants.NAR_TYPE + ":" + "${aol}" + "-" + Library.EXECUTABLE);
175       narInfo.setBinding(new AOL(binAOL[i]), Library.EXECUTABLE);
176       narInfo.setBinding(null, Library.EXECUTABLE);
177     }
178 
179     final File libDir = new File(baseDir, "lib");
180     final String[] libAOL = libDir.list();
181     for (int i = 0; libAOL != null && i < libAOL.length; i++) {
182       String bindingType = null;
183       final String[] libType = new File(libDir, libAOL[i]).list();
184       for (int j = 0; libType != null && j < libType.length; j++) {
185         narInfo.setNar(null, libType[j], project.getGroupId() + ":" + project.getArtifactId() + ":"
186             + NarConstants.NAR_TYPE + ":" + "${aol}" + "-" + libType[j]);
187 
188         // set if not set or override if SHARED
189         if (bindingType == null || libType[j].equals(Library.SHARED)) {
190           bindingType = libType[j];
191         }
192       }
193 
194       final AOL aol = new AOL(libAOL[i]);
195       if (mojo.getLibsName() != null) {
196         narInfo.setLibs(aol, mojo.getLibsName());
197       }
198       if (narInfo.getBinding(aol, null) == null) {
199         narInfo.setBinding(aol, bindingType != null ? bindingType : Library.NONE);
200       }
201       if (narInfo.getBinding(null, null) == null) {
202         narInfo.setBinding(null, bindingType != null ? bindingType : Library.NONE);
203       }
204     }
205   }
206 
207   @Override
208   public void unpackNar(final File unpackDir, final ArchiverManager archiverManager, final File file, final String os,
209       final String linkerName, final AOL defaultAOL) throws MojoExecutionException, MojoFailureException {
210     final File flagFile = new File(unpackDir, FileUtils.basename(file.getPath(), "." + NarConstants.NAR_EXTENSION)
211         + ".flag");
212 
213     boolean process = false;
214     if (!unpackDir.exists()) {
215       unpackDir.mkdirs();
216       process = true;
217     } else if (!flagFile.exists()) {
218       process = true;
219     } else if (file.lastModified() > flagFile.lastModified()) {
220       process = true;
221     }
222 
223     if (process) {
224       try {
225         unpackNarAndProcess(archiverManager, file, unpackDir, os, linkerName, defaultAOL);
226         FileUtils.fileDelete(flagFile.getPath());
227         FileUtils.fileWrite(flagFile.getPath(), "");
228       } catch (final IOException e) {
229         throw new MojoFailureException("Cannot create flag file: " + flagFile.getPath(), e);
230       }
231     }
232   }
233 }