1 17 package org.apache.servicemix.maven.plugin.jbi; 18 19 import java.io.File ; 20 import java.io.IOException ; 21 import java.util.Iterator ; 22 import java.util.Set ; 23 24 import org.apache.maven.archiver.MavenArchiveConfiguration; 25 import org.apache.maven.archiver.MavenArchiver; 26 import org.apache.maven.artifact.Artifact; 27 import org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter; 28 import org.apache.maven.plugin.MojoExecutionException; 29 import org.apache.maven.plugin.MojoFailureException; 30 import org.codehaus.plexus.archiver.jar.JarArchiver; 31 import org.codehaus.plexus.util.DirectoryScanner; 32 import org.codehaus.plexus.util.FileUtils; 33 34 45 public class GenerateSharedLibraryMojo extends AbstractJbiMojo { 46 47 53 private File outputDirectory; 54 55 61 private String sharedLibraryName; 62 63 69 private String jarName; 70 71 77 private JarArchiver jarArchiver; 78 79 85 private File jbiSourceDirectory; 86 87 92 private MavenArchiveConfiguration archive = new MavenArchiveConfiguration(); 93 94 public void execute() throws MojoExecutionException, MojoFailureException { 95 96 getLog().debug(" ======= GenerateInstallerMojo settings ======="); 97 getLog().debug("workDirectory[" + workDirectory + "]"); 98 getLog().debug("installerName[" + sharedLibraryName + "]"); 99 getLog().debug("jbiSourceDirectory[" + jbiSourceDirectory + "]"); 100 101 try { 102 103 createUnpackedSharedLibrary(); 104 105 File installerFile = new File (outputDirectory, sharedLibraryName); 106 createArchive(installerFile); 107 108 projectHelper.attachArtifact(project,"jar", "", new File ( 109 outputDirectory, jarName)); 110 111 projectHelper.attachArtifact(project,"zip", "installer", new File ( 112 outputDirectory, sharedLibraryName)); 113 114 } catch (JbiPluginException e) { 115 throw new MojoExecutionException("Failed to create shared library", 116 e); 117 } 118 } 119 120 private void createArchive(File installerFile) throws JbiPluginException { 121 try { 122 123 getLog().info( 125 "Generating shared library " 126 + installerFile.getAbsolutePath()); 127 MavenArchiver archiver = new MavenArchiver(); 128 archiver.setArchiver(jarArchiver); 129 archiver.setOutputFile(installerFile); 130 jarArchiver.addDirectory(workDirectory); 131 if (jbiSourceDirectory.isDirectory()) { 132 jarArchiver.addDirectory(jbiSourceDirectory, null, 133 DirectoryScanner.DEFAULTEXCLUDES); 134 } 135 archiver.createArchive(getProject(), archive); 137 138 } catch (Exception e) { 139 throw new JbiPluginException("Error creating shared library: " 140 + e.getMessage(), e); 141 } 142 } 143 144 private void createUnpackedSharedLibrary() throws JbiPluginException { 145 146 if (!workDirectory.isDirectory()) { 147 if (!workDirectory.mkdirs()) { 148 throw new JbiPluginException( 149 "Unable to create work directory: " + workDirectory); 150 } 151 } 152 153 File projectArtifact = new File (outputDirectory, project 154 .getArtifactId() 155 + "-" + project.getVersion() + ".jar"); 156 try { 157 FileUtils.copyFileToDirectory(projectArtifact, new File ( 158 workDirectory, LIB_DIRECTORY)); 159 160 } catch (IOException e) { 161 throw new JbiPluginException("Unable to copy file " 162 + projectArtifact, e); 163 } 164 165 Set artifacts = project.getArtifacts(); 166 for (Iterator iter = artifacts.iterator(); iter.hasNext();) { 167 Artifact artifact = (Artifact) iter.next(); 168 169 ScopeArtifactFilter filter = new ScopeArtifactFilter( 171 Artifact.SCOPE_RUNTIME); 172 if (!artifact.isOptional() && filter.include(artifact)) { 173 String type = artifact.getType(); 174 if ("jar".equals(type)) { 175 try { 176 FileUtils.copyFileToDirectory(artifact.getFile(), 177 new File (workDirectory, LIB_DIRECTORY)); 178 } catch (IOException e) { 179 throw new JbiPluginException("Unable to copy file " 180 + artifact.getFile(), e); 181 } 182 } 183 } 184 } 185 } 186 187 } 188 | Popular Tags |