1 19 20 package org.apache.geronimo.mavenplugins.car; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.io.FileOutputStream ; 25 import java.io.BufferedOutputStream ; 26 27 import java.util.Iterator ; 28 import java.util.Properties ; 29 import java.util.Map ; 30 31 import org.apache.geronimo.genesis.MojoSupport; 32 import org.apache.geronimo.genesis.util.ArtifactItem; 33 import org.apache.geronimo.genesis.dependency.DependencyHelper; 34 import org.apache.geronimo.genesis.dependency.DependencyTree; 35 import org.apache.geronimo.genesis.dependency.DependencyTree.Node; 36 37 import org.apache.maven.plugin.MojoExecutionException; 38 import org.apache.maven.plugin.MojoFailureException; 39 import org.apache.maven.project.MavenProject; 40 import org.apache.maven.project.MavenProjectHelper; 41 import org.apache.maven.artifact.Artifact; 42 import org.apache.maven.artifact.repository.ArtifactRepository; 43 44 49 public abstract class AbstractCarMojo 50 extends MojoSupport 51 { 52 59 protected MavenProject project; 60 61 68 protected File basedir; 69 70 77 protected MavenProjectHelper projectHelper; 78 79 82 protected DependencyHelper dependencyHelper = null; 83 84 88 protected MavenProject getProject() { 89 return project; 90 } 91 92 97 protected ArtifactRepository artifactRepository = null; 98 99 protected ArtifactRepository getArtifactRepository() { 100 return artifactRepository; 101 } 102 103 protected void init() throws MojoExecutionException, MojoFailureException { 104 super.init(); 105 106 dependencyHelper.setArtifactRepository(artifactRepository); 107 } 108 109 112 protected void generateExplicitVersionProperties(final File outputFile, DependencyTree dependencies) throws MojoExecutionException, IOException { 113 log.debug("Generating explicit version properties: " + outputFile); 114 115 Properties props = new Properties (); 117 118 try { 119 120 Node root = dependencies.getRootNode(); 121 122 Iterator children = root.getChildren().iterator(); 124 while (children.hasNext()) { 125 Node child = (Node) children.next(); 126 appendExplicitVersionProperties(child, props); 127 } 128 } 129 catch (Exception e) { 130 throw new MojoExecutionException("Failed to determine project dependencies", e); 131 } 132 133 BufferedOutputStream output = new BufferedOutputStream (new FileOutputStream (outputFile)); 134 props.store(output, null); 135 output.flush(); 136 output.close(); 137 } 138 139 private void appendExplicitVersionProperties(final Node node, final Properties props) { 140 assert node != null; 141 assert props != null; 142 143 Artifact artifact = node.getArtifact(); 144 if ("test".equals(artifact.getScope())) { 145 if (log.isDebugEnabled()) { 146 log.debug("Skipping artifact with scope test: " + artifact); 147 } 148 return; 149 } 150 151 String name = artifact.getGroupId() + "/" + artifact.getArtifactId() + "//" + artifact.getType(); 152 String value = artifact.getGroupId() + "/" + artifact.getArtifactId() + "/" + artifact.getVersion() + "/" + artifact.getType(); 153 154 if (log.isDebugEnabled()) { 155 log.debug("Setting " + name + "=" + value); 156 } 157 props.setProperty(name, value); 158 159 if (!node.getChildren().isEmpty()) { 160 Iterator children = node.getChildren().iterator(); 161 162 while (children.hasNext()) { 163 Node child = (Node) children.next(); 164 appendExplicitVersionProperties(child, props); 165 } 166 } 167 } 168 169 protected static File getArchiveFile(final File basedir, final String finalName, String classifier) { 170 if (classifier == null) { 171 classifier = ""; 172 } 173 else if (classifier.trim().length() > 0 && !classifier.startsWith("-")) { 174 classifier = "-" + classifier; 175 } 176 177 return new File (basedir, finalName + classifier + ".car"); 178 } 179 180 184 protected org.apache.geronimo.kernel.repository.Artifact mavenToGeronimoArtifact(final org.apache.maven.artifact.Artifact artifact) { 185 assert artifact != null; 186 187 return new org.apache.geronimo.kernel.repository.Artifact(artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(), artifact.getType()); 188 } 189 190 protected org.apache.maven.artifact.Artifact geronimoToMavenArtifact(final org.apache.geronimo.kernel.repository.Artifact artifact) throws MojoExecutionException { 191 assert artifact != null; 192 193 ArtifactItem item = new ArtifactItem(); 194 item.setGroupId(artifact.getGroupId()); 195 item.setArtifactId(artifact.getArtifactId()); 196 item.setVersion(artifact.getVersion().toString()); 197 item.setType(artifact.getType()); 198 199 return createArtifact(item); 200 } 201 202 208 protected boolean isModuleArtifact(final org.apache.geronimo.kernel.repository.Artifact artifact) { 209 assert artifact != null; 210 211 return "car".equals(artifact.getType()); 212 } 213 214 protected class ArtifactLookupImpl 215 implements Maven2RepositoryAdapter.ArtifactLookup 216 { 217 218 private final Map resolvedArtifacts; 219 220 public ArtifactLookupImpl(Map resolvedArtifacts) { 221 this.resolvedArtifacts = resolvedArtifacts; 222 } 223 224 public File getBasedir() { 225 String path = getArtifactRepository().getBasedir(); 226 return new File (path); 227 } 228 229 private boolean isProjectArtifact(final org.apache.geronimo.kernel.repository.Artifact artifact) { 230 MavenProject project = getProject(); 231 232 return artifact.getGroupId().equals(project.getGroupId()) && 233 artifact.getArtifactId().equals(project.getArtifactId()); 234 } 235 236 public File getLocation(final org.apache.geronimo.kernel.repository.Artifact artifact) { 237 assert artifact != null; 238 239 boolean debug = log.isDebugEnabled(); 240 241 Artifact mavenArtifact = (Artifact)resolvedArtifacts.get(artifact); 242 243 if (mavenArtifact == null) { 245 mavenArtifact = getArtifactFactory().createArtifact( 246 artifact.getGroupId(), 247 artifact.getArtifactId(), 248 artifact.getVersion().toString(), 249 null, 250 artifact.getType() 251 ); 252 } 253 254 if (isProjectArtifact(artifact)) { 256 if (debug) { 257 log.debug("Skipping resolution of project artifact: " + artifact); 258 } 259 260 return new File (getBasedir(), getArtifactRepository().pathOf(mavenArtifact)); 266 } 267 268 File file; 269 try { 270 if (!mavenArtifact.isResolved()) { 271 if (debug) { 272 log.debug("Resolving artifact: " + mavenArtifact); 273 } 274 mavenArtifact = resolveArtifact(mavenArtifact); 275 276 resolvedArtifacts.put(artifact, mavenArtifact); 278 } 279 280 String path = getArtifactRepository().pathOf(mavenArtifact); 286 file = new File (getBasedir(), path); 287 file = new File (mavenArtifact.getFile().getParentFile(), file.getName()); 288 } 289 catch (MojoExecutionException e) { 290 throw new RuntimeException ("Failed to resolve: " + mavenArtifact, e); 291 } 292 293 return file; 294 } 295 } 296 297 } 298 | Popular Tags |