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.FileUtils; 38 39 50 public class GenerateServiceUnitMojo extends AbstractJbiMojo { 51 52 58 private String serviceUnitName; 59 60 66 private File outputDirectory; 67 68 74 private JarArchiver jarArchiver; 75 76 81 private MavenArchiveConfiguration archive = new MavenArchiveConfiguration(); 82 83 88 private File serviceUnitLocation; 89 90 public void execute() throws MojoExecutionException, MojoFailureException { 91 try { 92 93 createUnpackedInstaller(); 94 95 File serviceUnitFile = new File (outputDirectory, serviceUnitName); 96 createArchive(serviceUnitFile); 97 98 projectHelper.attachArtifact(project, "zip", "", new File ( 99 outputDirectory, serviceUnitName)); 100 101 } catch (JbiPluginException e) { 102 throw new MojoExecutionException("Failed to create service unit", e); 103 } 104 105 } 106 107 private void createArchive(File installerFile) throws JbiPluginException { 108 try { 109 110 getLog().info( 112 "Generating service unit " 113 + installerFile.getAbsolutePath()); 114 MavenArchiver archiver = new MavenArchiver(); 115 archiver.setArchiver(jarArchiver); 116 archiver.setOutputFile(installerFile); 117 jarArchiver.addDirectory(workDirectory); 118 119 archiver.createArchive(getProject(), archive); 121 122 } catch (ArchiverException e) { 123 throw new JbiPluginException("Error creating service unit: " 124 + e.getMessage(), e); 125 } catch (ManifestException e) { 126 throw new JbiPluginException("Error creating service unit: " 127 + e.getMessage(), e); 128 } catch (IOException e) { 129 throw new JbiPluginException("Error creating service unit: " 130 + e.getMessage(), e); 131 } catch (DependencyResolutionRequiredException e) { 132 throw new JbiPluginException("Error creating service unit: " 133 + e.getMessage(), e); 134 } 135 136 } 137 138 private void createUnpackedInstaller() throws JbiPluginException { 139 140 if (!workDirectory.isDirectory()) { 141 if (!workDirectory.mkdirs()) { 142 throw new JbiPluginException( 143 "Unable to create work directory: " + workDirectory); 144 } 145 } 146 147 try { 148 FileUtils.copyDirectoryStructure(serviceUnitLocation, workDirectory); 149 } catch (IOException e) { 150 throw new JbiPluginException("Unable to copy directory " 151 + serviceUnitLocation, e); 152 } 153 154 ScopeArtifactFilter filter = new ScopeArtifactFilter( 155 Artifact.SCOPE_RUNTIME); 156 157 JbiResolutionListener listener = resolveProject(); 158 160 Set includes = new HashSet (); 161 for (Iterator iter = project.getArtifacts().iterator(); iter.hasNext();) { 162 Artifact artifact = (Artifact) iter.next(); 163 if (!artifact.isOptional() && filter.include(artifact)) { 164 MavenProject project = null; 165 getLog().info("Resolving "+artifact); 166 try { 167 project = projectBuilder.buildFromRepository(artifact, 168 remoteRepos, localRepo); 169 } catch (ProjectBuildingException e) { 170 getLog().warn( 171 "Unable to determine packaging for dependency : " 172 + artifact.getArtifactId() 173 + " assuming jar"); 174 } 175 String type = project != null ? project.getPackaging() 176 : artifact.getType(); 177 if ("jbi-component".equals(type)) { 178 removeBranch(listener, artifact); 179 } else if ("jbi-shared-library".equals(type)) { 180 removeBranch(listener, artifact); 181 } else if ("jar".equals(type)) { 182 includes.add(artifact); 183 } 184 } 185 } 186 188 for (Iterator iter = retainArtifacts(includes, listener).iterator(); iter 189 .hasNext();) { 190 Artifact artifact = (Artifact) iter.next(); 191 try { 192 getLog().info("Including: " + artifact); 193 FileUtils.copyFileToDirectory(artifact.getFile(), new File ( 194 workDirectory, LIB_DIRECTORY)); 195 } catch (IOException e) { 196 throw new JbiPluginException("Unable to copy file " 197 + artifact.getFile(), e); 198 } 199 } 200 } 201 } 202 | Popular Tags |