artifactToNameMap.put(key, name);
}
}
public void execute() throws MojoExecutionException {
Log log = getLog();
try {
// Create the target directory
File root;
if (targetDirectory == null) {
root = new File(project.getBuild().getDirectory(), "plugins/");
} else {
root = targetDirectory;
}
root.mkdirs();
// Build sets of exclude directories and included/excluded/groupids
Set<String> excludedFileNames = new HashSet<String>();
if (excludeDirectories != null) {
for (File f : excludeDirectories) {
if (f.isDirectory()) {
for (String n : f.list()) {
excludedFileNames.add(n);
}
}
}
}
Set<String> includedGroupIds = new HashSet<String>();
if (includeGroupIds != null) {
for (String g : includeGroupIds) {
includedGroupIds.add(g);
}
}
Set<String> excludedGroupIds = new HashSet<String>();
if (excludeGroupIds != null) {
for (String g : excludeGroupIds) {
excludedGroupIds.add(g);
}
}
// Find all the distribution poms
List<MavenProject> poms = new ArrayList<MavenProject>();
poms.add(project);
if (useDistributionName) {
for (Object o : project.getArtifacts()) {
Artifact artifact = (Artifact)o;
if ("pom".equals(artifact.getType()) && artifact.getGroupId().equals(project.getGroupId())
&& artifact.getArtifactId().startsWith("tuscany-feature-")) {
log.info("Dependent distribution: " + artifact);
MavenProject pomProject = buildProject(artifact);
poms.add(pomProject);
// log.info(pomProject.getArtifactMap().toString());
}
}
}
// Process all the dependency artifacts
ProjectSet bundleSymbolicNames = new ProjectSet(poms);
ProjectSet bundleLocations = new ProjectSet(poms);
ProjectSet jarNames = new ProjectSet(poms);
for (Object o : project.getArtifacts()) {
Artifact artifact = (Artifact)o;
// Only consider Compile and Runtime dependencies
if (!(Artifact.SCOPE_COMPILE.equals(artifact.getScope()) || Artifact.SCOPE_RUNTIME.equals(artifact
.getScope())
|| Artifact.SCOPE_PROVIDED.equals(artifact.getScope()) || (generateTargetPlatform && Artifact.SCOPE_TEST
.equals(artifact.getScope())))) {
log.info("Skipping artifact: " + artifact);
continue;
}
// Only consider JAR and WAR files
if (!"jar".equals(artifact.getType()) && !"war".equals(artifact.getType())) {
continue;
}
// Exclude artifact if its groupId is excluded or if it's not included
if (excludedGroupIds.contains(artifact.getGroupId())) {
log.debug("Artifact groupId is excluded: " + artifact);
continue;
}
if (!includedGroupIds.isEmpty()) {
if (!includedGroupIds.contains(artifact.getGroupId())) {
log.debug("Artifact groupId is not included: " + artifact);
continue;
}
}
File artifactFile = artifact.getFile();
if (!artifactFile.exists()) {
log.warn("Artifact doesn't exist: " + artifact);
continue;
}
if (log.isDebugEnabled()) {
log.debug("Processing artifact: " + artifact);
}
// Get the bundle name if the artifact is an OSGi bundle
String bundleName = null;
try {
bundleName = BundleUtil.getBundleSymbolicName(artifact.getFile());
} catch (IOException e) {
throw new MojoExecutionException(e.getMessage(), e);
}
if (bundleName != null) {
// Exclude artifact if its file name is excluded
if (excludedFileNames.contains(artifactFile.getName())) {
log.debug("Artifact file is excluded: " + artifact);
continue;
}
// Copy an OSGi bundle as is
log.info("Adding OSGi bundle artifact: " + artifact);
copyFile(artifactFile, root);
bundleSymbolicNames.add(artifact, bundleName);
bundleLocations.add(artifact, artifactFile.getName());
jarNames.add(artifact, artifactFile.getName());
} else if ("war".equals(artifact.getType())) {
// Exclude artifact if its file name is excluded
if (excludedFileNames.contains(artifactFile.getName())) {
log.debug("Artifact file is excluded: " + artifact);
continue;
}
// Copy a WAR as is
log.info("Adding WAR artifact: " + artifact);
copyFile(artifactFile, root);
} else {
// String version = BundleUtil.osgiVersion(artifact.getVersion());
// String symbolicName = (artifact.getGroupId() + "." + artifact.getArtifactId());
// String dirName = symbolicName + "_" + version;
String dirName = artifactFile.getName().substring(0, artifactFile.getName().length() - 4);
File dir = new File(root, dirName);
// Exclude artifact if its file name is excluded
if (excludedFileNames.contains(dir.getName())) {
log.debug("Artifact file is excluded: " + artifact);
continue;
}
if (artifactAggregations != null) {
boolean aggregated = false;
for (ArtifactAggregation group : artifactAggregations) {
if (group.matches(artifact)) {
group.getArtifacts().add(artifact);
aggregated = true;
break;
}
}
if (aggregated) {
continue;
}
}
// Create a bundle directory for a non-OSGi JAR
log.info("Adding JAR artifact: " + artifact);
String version = BundleUtil.osgiVersion(artifact.getVersion());
Set<File> jarFiles = new HashSet<File>();
jarFiles.add(artifactFile);
String symbolicName = (artifact.getGroupId() + "." + artifact.getArtifactId());
Manifest mf = BundleUtil.libraryManifest(jarFiles, symbolicName, symbolicName, version, null);
File file = new File(dir, "META-INF");
file.mkdirs();
file = new File(file, "MANIFEST.MF");
FileOutputStream fos = new FileOutputStream(file);
write(mf, fos);
fos.close();
copyFile(artifactFile, dir);
bundleSymbolicNames.add(artifact, symbolicName);
bundleLocations.add(artifact, dir.getName());
jarNames.add(artifact, dirName + "/" + artifactFile.getName());
}
}
if (artifactAggregations != null) {
for (ArtifactAggregation group : artifactAggregations) {
if (group.getArtifacts().isEmpty()) {
continue;
}
String symbolicName = group.getSymbolicName();
String version = group.getVersion();
File dir = new File(root, symbolicName + "-" + version);
dir.mkdir();
Set<File> jarFiles = new HashSet<File>();
Artifact artifact = null;
for (Artifact a : group.getArtifacts()) {
log.info("Aggragating JAR artifact: " + a);
artifact = a;
jarFiles.add(a.getFile());
copyFile(a.getFile(), dir);
jarNames.add(a, symbolicName + "-" + version + "/" + a.getFile().getName());
}