1 17 package org.apache.servicemix.maven.plugin.jbi; 18 19 import java.io.File ; 20 import java.io.IOException ; 21 import java.util.ArrayList ; 22 import java.util.HashSet ; 23 import java.util.Iterator ; 24 import java.util.List ; 25 import java.util.Set ; 26 27 import org.apache.maven.artifact.Artifact; 28 import org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter; 29 import org.apache.maven.plugin.MojoExecutionException; 30 import org.apache.maven.plugin.MojoFailureException; 31 import org.apache.maven.project.MavenProject; 32 import org.apache.maven.project.ProjectBuildingException; 33 import org.codehaus.plexus.util.FileUtils; 34 35 46 public class GenerateComponentDescriptorMojo extends AbstractJbiMojo { 47 48 public static final String UTF_8 = "UTF-8"; 49 50 55 private Boolean generateJbiDescriptor = Boolean.TRUE; 56 57 63 private String component; 64 65 71 private String bootstrap; 72 73 79 private String type; 80 81 86 private String name; 87 88 93 private String description; 94 95 100 private String encoding = UTF_8; 101 102 107 private String generatedDescriptorLocation; 108 109 114 private String componentClassLoaderDelegation; 115 116 121 private String bootstrapClassLoaderDelegation; 122 123 public void execute() throws MojoExecutionException, MojoFailureException { 124 125 getLog().debug( 126 " ======= GenerateComponentDescriptorMojo settings ======="); 127 getLog().debug("workDirectory[" + workDirectory + "]"); 128 getLog().debug("generateJbiDescriptor[" + generateJbiDescriptor + "]"); 129 getLog().debug("type[" + type + "]"); 130 getLog().debug("component[" + component + "]"); 131 getLog().debug("bootstrap[" + bootstrap + "]"); 132 getLog().debug("name[" + name + "]"); 133 getLog().debug("description[" + description + "]"); 134 getLog().debug("encoding[" + encoding + "]"); 135 getLog().debug( 136 "generatedDescriptorLocation[" + generatedDescriptorLocation 137 + "]"); 138 139 if (!generateJbiDescriptor.booleanValue()) { 140 getLog().debug("Generation of jbi.xml is disabled"); 141 return; 142 } 143 144 getLog().info("Generating jbi.xml"); 146 try { 147 generateJbiDescriptor(); 148 } catch (JbiPluginException e) { 149 throw new MojoExecutionException("Failed to generate jbi.xml", e); 150 } 151 152 try { 153 FileUtils.copyFileToDirectory(new File (generatedDescriptorLocation, 154 JBI_DESCRIPTOR), new File (getWorkDirectory(), META_INF)); 155 } catch (IOException e) { 156 throw new MojoExecutionException( 157 "Unable to copy jbi.xml to final destination", e); 158 } 159 } 160 161 164 protected void generateJbiDescriptor() throws JbiPluginException { 165 File outputDir = new File (generatedDescriptorLocation); 166 if (!outputDir.exists()) { 167 outputDir.mkdirs(); 168 } 169 170 File descriptor = new File (outputDir, JBI_DESCRIPTOR); 171 172 List uris = new ArrayList (); 173 DependencyInformation info = new DependencyInformation(); 174 info.setFilename(LIB_DIRECTORY + "/" + project.getArtifactId() + "-" 175 + project.getVersion() + ".jar"); 176 info.setVersion(project.getVersion()); 177 info.setName(project.getArtifactId()); 178 info.setType("jar"); 179 uris.add(info); 180 181 ScopeArtifactFilter filter = new ScopeArtifactFilter( 182 Artifact.SCOPE_RUNTIME); 183 184 JbiResolutionListener listener = resolveProject(); 185 187 Set includes = new HashSet (); 188 for (Iterator iter = project.getArtifacts().iterator(); iter.hasNext();) { 189 Artifact artifact = (Artifact) iter.next(); 190 if (!artifact.isOptional() && filter.include(artifact)) { 191 MavenProject project = null; 192 try { 193 project = projectBuilder.buildFromRepository(artifact, 194 remoteRepos, localRepo); 195 } catch (ProjectBuildingException e) { 196 getLog().warn( 197 "Unable to determine packaging for dependency : " 198 + artifact.getArtifactId() 199 + " assuming jar"); 200 } 201 String type = project != null ? project.getPackaging() 202 : artifact.getType(); 203 if ("jbi-shared-library".equals(type)) { 204 removeChildren(listener, artifact); 205 includes.add(artifact); 206 } else if ("jar".equals(type)) { 207 includes.add(artifact); 208 } 209 } 210 } 211 213 for (Iterator iter = retainArtifacts(includes, listener).iterator(); iter 214 .hasNext();) { 215 Artifact artifact = (Artifact) iter.next(); 216 MavenProject project = null; 217 try { 218 project = projectBuilder.buildFromRepository(artifact, 219 remoteRepos, localRepo); 220 } catch (ProjectBuildingException e) { 221 getLog().warn( 222 "Unable to determine packaging for dependency : " 223 + artifact.getArtifactId() + " assuming jar"); 224 } 225 String type = project != null ? project.getPackaging() : artifact 226 .getType(); 227 info = new DependencyInformation(); 228 info 229 .setFilename(LIB_DIRECTORY + "/" 230 + artifact.getFile().getName()); 231 info.setVersion(artifact.getVersion()); 232 info.setName(artifact.getArtifactId()); 233 info.setType(type); 234 uris.add(info); 235 } 236 237 JbiComponentDescriptorWriter writer = new JbiComponentDescriptorWriter( 238 encoding); 239 writer.write(descriptor, component, bootstrap, type, name, description, 240 componentClassLoaderDelegation, bootstrapClassLoaderDelegation, 241 uris); 242 } 243 } 244 | Popular Tags |