/src/main/java/org/apache/maven/plugin/nar/NarLayout21.java

https://github.com/mirkojahn/maven-nar-plugin · Java · 221 lines · 140 code · 26 blank · 55 comment · 24 complexity · 0c356c3a0749ea57e447279025ed61ec MD5 · raw file

  1. package org.apache.maven.plugin.nar;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import org.apache.maven.plugin.MojoExecutionException;
  5. import org.apache.maven.plugin.MojoFailureException;
  6. import org.apache.maven.plugin.logging.Log;
  7. import org.apache.maven.project.MavenProject;
  8. import org.apache.maven.project.MavenProjectHelper;
  9. import org.codehaus.plexus.archiver.manager.ArchiverManager;
  10. import org.codehaus.plexus.util.FileUtils;
  11. /*
  12. * Licensed to the Apache Software Foundation (ASF) under one
  13. * or more contributor license agreements. See the NOTICE file
  14. * distributed with this work for additional information
  15. * regarding copyright ownership. The ASF licenses this file
  16. * to you under the Apache License, Version 2.0 (the
  17. * "License"); you may not use this file except in compliance
  18. * with the License. You may obtain a copy of the License at
  19. *
  20. * http://www.apache.org/licenses/LICENSE-2.0
  21. *
  22. * Unless required by applicable law or agreed to in writing,
  23. * software distributed under the License is distributed on an
  24. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  25. * KIND, either express or implied. See the License for the
  26. * specific language governing permissions and limitations
  27. * under the License.
  28. */
  29. /**
  30. * Layout which expands a nar file into:
  31. *
  32. * <pre>
  33. * nar/noarch/include
  34. * nar/aol/<aol>-<type>/bin
  35. * nar/aol/<aol>-<type>/lib
  36. * </pre>
  37. *
  38. * This loayout has a one-to-one relation with the aol-type version of the nar.
  39. *
  40. * @author Mark Donszelmann (Mark.Donszelmann@gmail.com)
  41. */
  42. public class NarLayout21
  43. extends AbstractNarLayout
  44. {
  45. private NarFileLayout fileLayout;
  46. public NarLayout21( Log log )
  47. {
  48. super( log );
  49. this.fileLayout = new NarFileLayout10();
  50. }
  51. public File getNoArchDirectory( File baseDir, String artifactId, String version )
  52. {
  53. return new File( baseDir, artifactId + "-" + version + "-" + NarConstants.NAR_NO_ARCH );
  54. }
  55. private File getAolDirectory( File baseDir, String artifactId, String version, String aol, String type )
  56. {
  57. return new File( baseDir, artifactId + "-" + version + "-" + aol + "-" + type );
  58. }
  59. /*
  60. * (non-Javadoc)
  61. * @see org.apache.maven.plugin.nar.NarLayout#getIncludeDirectory(java.io.File)
  62. */
  63. public final File getIncludeDirectory( File baseDir, String artifactId, String version )
  64. {
  65. return new File( getNoArchDirectory( baseDir, artifactId, version ), fileLayout.getIncludeDirectory() );
  66. }
  67. /*
  68. * (non-Javadoc)
  69. * @see org.apache.maven.plugin.nar.NarLayout#getLibDir(java.io.File, org.apache.maven.plugin.nar.AOL,
  70. * java.lang.String)
  71. */
  72. public final File getLibDirectory( File baseDir, String artifactId, String version, String aol, String type )
  73. throws MojoExecutionException
  74. {
  75. if ( type.equals( Library.EXECUTABLE ) )
  76. {
  77. throw new MojoExecutionException(
  78. "NAR: for type EXECUTABLE call getBinDirectory instead of getLibDirectory" );
  79. }
  80. File dir = getAolDirectory( baseDir, artifactId, version, aol, type );
  81. dir = new File( dir, fileLayout.getLibDirectory( aol, type ) );
  82. return dir;
  83. }
  84. /*
  85. * (non-Javadoc)
  86. * @see org.apache.maven.plugin.nar.NarLayout#getLibDir(java.io.File, org.apache.maven.plugin.nar.AOL,
  87. * java.lang.String)
  88. */
  89. public final File getBinDirectory( File baseDir, String artifactId, String version, String aol )
  90. {
  91. File dir = getAolDirectory( baseDir, artifactId, version, aol, Library.EXECUTABLE );
  92. dir = new File( dir, fileLayout.getBinDirectory( aol ) );
  93. return dir;
  94. }
  95. /*
  96. * (non-Javadoc)
  97. * @see org.apache.maven.plugin.nar.NarLayout#attachNars(java.io.File, org.apache.maven.project.MavenProjectHelper,
  98. * org.apache.maven.project.MavenProject, org.apache.maven.plugin.nar.NarInfo)
  99. */
  100. public final void attachNars( File baseDir, ArchiverManager archiverManager, MavenProjectHelper projectHelper,
  101. MavenProject project, NarInfo narInfo )
  102. throws MojoExecutionException
  103. {
  104. if ( getNoArchDirectory( baseDir, project.getArtifactId(), project.getVersion() ).exists() )
  105. {
  106. attachNar( archiverManager, projectHelper, project, NarConstants.NAR_NO_ARCH,
  107. getNoArchDirectory( baseDir, project.getArtifactId(), project.getVersion() ), "*/**" );
  108. narInfo.setNar( null, NarConstants.NAR_NO_ARCH, project.getGroupId() + ":" + project.getArtifactId() + ":"
  109. + NarConstants.NAR_TYPE + ":" + NarConstants.NAR_NO_ARCH );
  110. }
  111. // list all directories in basedir, scan them for classifiers
  112. String[] subDirs = baseDir.list();
  113. for ( int i = 0; ( subDirs != null ) && ( i < subDirs.length ); i++ )
  114. {
  115. String artifactIdVersion = project.getArtifactId() + "-" + project.getVersion();
  116. // skip entries not belonging to this project
  117. if ( !subDirs[i].startsWith( artifactIdVersion ) )
  118. continue;
  119. String classifier = subDirs[i].substring( artifactIdVersion.length() + 1 );
  120. // skip noarch here
  121. if ( classifier.equals( NarConstants.NAR_NO_ARCH ) )
  122. continue;
  123. File dir = new File( baseDir, subDirs[i] );
  124. attachNar( archiverManager, projectHelper, project, classifier, dir, "*/**" );
  125. int lastDash = classifier.lastIndexOf( '-' );
  126. String type = classifier.substring( lastDash + 1 );
  127. AOL aol = new AOL( classifier.substring( 0, lastDash - 1 ) );
  128. if ( type.equals( Library.EXECUTABLE ) )
  129. {
  130. if ( narInfo.getBinding( aol, null ) == null )
  131. {
  132. narInfo.setBinding( aol, Library.EXECUTABLE );
  133. }
  134. if ( narInfo.getBinding( null, null ) == null )
  135. {
  136. narInfo.setBinding( null, Library.EXECUTABLE );
  137. }
  138. }
  139. else
  140. {
  141. // and not set or override if SHARED
  142. if ( ( narInfo.getBinding( aol, null ) == null ) || !type.equals( Library.SHARED ) )
  143. {
  144. narInfo.setBinding( aol, type );
  145. }
  146. // and not set or override if SHARED
  147. if ( ( narInfo.getBinding( null, null ) == null ) || !type.equals( Library.SHARED ) )
  148. {
  149. narInfo.setBinding( null, type );
  150. }
  151. }
  152. narInfo.setNar( null, type, project.getGroupId() + ":" + project.getArtifactId() + ":"
  153. + NarConstants.NAR_TYPE + ":" + "${aol}" + "-" + type );
  154. }
  155. }
  156. public void unpackNar( File unpackDirectory, ArchiverManager archiverManager, File file, String os, String linkerName,
  157. AOL defaultAOL )
  158. throws MojoExecutionException, MojoFailureException
  159. {
  160. File dir = getNarUnpackDirectory(unpackDirectory, file);
  161. boolean process = false;
  162. if ( !unpackDirectory.exists() )
  163. {
  164. unpackDirectory.mkdirs();
  165. process = true;
  166. }
  167. else if ( !dir.exists() )
  168. {
  169. process = true;
  170. }
  171. else if ( file.lastModified() > dir.lastModified() )
  172. {
  173. try
  174. {
  175. FileUtils.deleteDirectory( dir );
  176. }
  177. catch ( IOException e )
  178. {
  179. throw new MojoExecutionException( "Could not delete directory: " + dir, e );
  180. }
  181. process = true;
  182. }
  183. if ( process )
  184. {
  185. unpackNarAndProcess( archiverManager, file, dir, os, linkerName, defaultAOL );
  186. }
  187. }
  188. public File getNarUnpackDirectory(File baseUnpackDirectory, File narFile)
  189. {
  190. File dir = new File(
  191. baseUnpackDirectory,
  192. FileUtils.basename( narFile.getPath(), "." + NarConstants.NAR_EXTENSION ));
  193. return dir;
  194. }
  195. }