1 17 package org.apache.servicemix.maven.plugin.jbi; 18 19 import java.io.File ; 20 import java.io.IOException ; 21 import java.util.HashSet ; 22 import java.util.Iterator ; 23 import java.util.Set ; 24 25 import org.apache.maven.archiver.MavenArchiveConfiguration; 26 import org.apache.maven.archiver.MavenArchiver; 27 import org.apache.maven.artifact.Artifact; 28 import org.apache.maven.artifact.DependencyResolutionRequiredException; 29 import org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter; 30 import org.apache.maven.plugin.MojoExecutionException; 31 import org.apache.maven.plugin.MojoFailureException; 32 import org.apache.maven.project.MavenProject; 33 import org.apache.maven.project.ProjectBuildingException; 34 import org.codehaus.plexus.archiver.ArchiverException; 35 import org.codehaus.plexus.archiver.jar.JarArchiver; 36 import org.codehaus.plexus.archiver.jar.ManifestException; 37 import org.codehaus.plexus.util.DirectoryScanner; 38 import org.codehaus.plexus.util.FileUtils; 39 40 51 public class GenerateComponentMojo extends AbstractJbiMojo { 52 53 59 private File outputDirectory; 60 61 67 private String installerName; 68 69 75 private JarArchiver jarArchiver; 76 77 83 private File jbiSourceDirectory; 84 85 90 private MavenArchiveConfiguration archive = new MavenArchiveConfiguration(); 91 92 public void execute() throws MojoExecutionException, MojoFailureException { 93 94 getLog().debug(" ======= GenerateInstallerMojo settings ======="); 95 getLog().debug("workDirectory[" + workDirectory + "]"); 96 getLog().debug("installerName[" + installerName + "]"); 97 getLog().debug("jbiSourceDirectory[" + jbiSourceDirectory + "]"); 98 99 try { 100 101 createUnpackedInstaller(); 102 103 File installerFile = new File (outputDirectory, installerName); 104 createArchive(installerFile); 105 106 projectHelper.attachArtifact(project, "zip", "installer", new File ( 107 outputDirectory, installerName)); 108 109 } catch (JbiPluginException e) { 110 throw new MojoExecutionException("Failed to create installer", e); 111 } 112 } 113 114 private void createArchive(File installerFile) throws JbiPluginException { 115 try { 116 117 getLog().info( 119 "Generating installer " + installerFile.getAbsolutePath()); 120 MavenArchiver archiver = new MavenArchiver(); 121 archiver.setArchiver(jarArchiver); 122 archiver.setOutputFile(installerFile); 123 jarArchiver.addDirectory(workDirectory); 124 if (jbiSourceDirectory.isDirectory()) { 125 jarArchiver.addDirectory(jbiSourceDirectory, null, 126 DirectoryScanner.DEFAULTEXCLUDES); 127 } 128 archiver.createArchive(getProject(), archive); 130 131 } catch (ArchiverException e) { 132 throw new JbiPluginException("Error creating assembly: " 133 + e.getMessage(), e); 134 } catch (ManifestException e) { 135 throw new JbiPluginException("Error creating assembly: " 136 + e.getMessage(), e); 137 } catch (IOException e) { 138 throw new JbiPluginException("Error creating assembly: " 139 + e.getMessage(), e); 140 } catch (DependencyResolutionRequiredException e) { 141 throw new JbiPluginException("Error creating assembly: " 142 + e.getMessage(), e); 143 } 144 145 } 146 147 private void createUnpackedInstaller() throws JbiPluginException { 148 149 if (!workDirectory.isDirectory()) { 150 if (!workDirectory.mkdirs()) { 151 throw new JbiPluginException( 152 "Unable to create work directory: " + workDirectory); 153 } 154 } 155 156 File projectArtifact = new File (outputDirectory, project 157 .getArtifactId() 158 + "-" + project.getVersion() + ".jar"); 159 try { 160 FileUtils.copyFileToDirectory(projectArtifact, new File ( 161 workDirectory, LIB_DIRECTORY)); 162 } catch (IOException e) { 163 throw new JbiPluginException("Unable to copy file " 164 + projectArtifact, e); 165 } 166 167 ScopeArtifactFilter filter = new ScopeArtifactFilter( 168 Artifact.SCOPE_RUNTIME); 169 170 JbiResolutionListener listener = resolveProject(); 171 173 Set includes = new HashSet (); 174 for (Iterator iter = project.getArtifacts().iterator(); iter.hasNext();) { 175 Artifact artifact = (Artifact) iter.next(); 176 if (!artifact.isOptional() && filter.include(artifact)) { 177 MavenProject project = null; 178 try { 179 project = projectBuilder.buildFromRepository(artifact, 180 remoteRepos, localRepo); 181 } catch (ProjectBuildingException e) { 182 getLog().warn( 183 "Unable to determine packaging for dependency : " 184 + artifact.getArtifactId() 185 + " assuming jar"); 186 } 187 String type = project != null ? project.getPackaging() 188 : artifact.getType(); 189 if ("jbi-shared-library".equals(type)) { 190 removeBranch(listener, artifact); 191 } else if ("jar".equals(type)) { 192 includes.add(artifact); 193 } 194 } 195 } 196 198 for (Iterator iter = retainArtifacts(includes, listener).iterator(); iter 199 .hasNext();) { 200 Artifact artifact = (Artifact) iter.next(); 201 try { 202 getLog().info("Including: " + artifact); 203 FileUtils.copyFileToDirectory(artifact.getFile(), new File ( 204 workDirectory, LIB_DIRECTORY)); 205 } catch (IOException e) { 206 throw new JbiPluginException("Unable to copy file " 207 + artifact.getFile(), e); 208 } 209 } 210 } 211 } 212 | Popular Tags |