public boolean isReferenceFromEar(IClasspathEntryDescriptor descriptor) { IClasspathEntry entry = descriptor.getClasspathEntry(); String scope = descriptor.getScope(); // these dependencies aren't added to the manifest cp // retain optional dependencies here, they might be used just to express the // dependency to be used in the manifest if (Artifact.SCOPE_PROVIDED.equals(scope) || Artifact.SCOPE_TEST.equals(scope) || Artifact.SCOPE_SYSTEM.equals(scope)) { return false; } // calculate in regard to includes/excludes whether this jar is // to be packaged into WEB-INF/lib String jarFileName = "WEB-INF/lib/" + entry.getPath().lastSegment(); return isExcludedFromWebInfLib(jarFileName); }
public void configureClasspath( IProject project, MavenProject mavenProject, IClasspathDescriptor classpath, IProgressMonitor monitor) throws CoreException { // Improve skinny war support by generating the manifest classpath // similar to mvn eclipse:eclipse // http://maven.apache.org/plugins/maven-war-plugin/examples/skinny-wars.html WarPluginConfiguration config = new WarPluginConfiguration(mavenProject, project); WarPackagingOptions opts = new WarPackagingOptions(config); StringBuilder manifestCp = new StringBuilder(); /* * Need to take care of three separate cases * * 1. remove any project dependencies (they are represented as J2EE module dependencies) * 2. add non-dependency attribute for entries originated by artifacts with * runtime, system, test scopes or optional dependencies (not sure about the last one) * 3. make sure all dependency JAR files have unique file names, i.e. artifactId/version collisions */ Set<String> dups = new LinkedHashSet<String>(); Set<String> names = new HashSet<String>(); // first pass removes projects, adds non-dependency attribute and collects colliding filenames Iterator<IClasspathEntryDescriptor> iter = classpath.getEntryDescriptors().iterator(); while (iter.hasNext()) { IClasspathEntryDescriptor descriptor = iter.next(); IClasspathEntry entry = descriptor.getClasspathEntry(); String scope = descriptor.getScope(); String key = ArtifactUtils.versionlessKey(descriptor.getGroupId(), descriptor.getArtifactId()); Artifact artifact = mavenProject.getArtifactMap().get(key); String extension = artifact.getArtifactHandler().getExtension(); if (IClasspathEntry.CPE_PROJECT == entry.getEntryKind() && Artifact.SCOPE_COMPILE.equals(scope)) { // get deployed name for project dependencies // TODO can this be done somehow more elegantly? IProject p = (IProject) ResourcesPlugin.getWorkspace().getRoot().findMember(entry.getPath()); IVirtualComponent component = ComponentCore.createComponent(p); boolean usedInEar = opts.isReferenceFromEar(component, extension); if (opts.isSkinnyWar() && usedInEar) { if (manifestCp.length() > 0) { manifestCp.append(" "); } // MNGECLIPSE-2393 prepend ManifestClasspath prefix if (config.getManifestClasspathPrefix() != null && !JEEPackaging.isJEEPackaging(artifact.getType())) { manifestCp.append(config.getManifestClasspathPrefix()); } manifestCp.append(component.getDeployedName()).append(".").append(extension); } if (!descriptor.isOptionalDependency() || usedInEar) { // remove mandatory project dependency from classpath iter.remove(); continue; } // else : optional dependency not used in ear -> need to trick ClasspathAttribute with // NONDEPENDENCY_ATTRIBUTE } if (opts.isSkinnyWar() && opts.isReferenceFromEar(descriptor)) { if (manifestCp.length() > 0) { manifestCp.append(" "); } if (config.getManifestClasspathPrefix() != null && !JEEPackaging.isJEEPackaging(artifact.getType())) { manifestCp.append(config.getManifestClasspathPrefix()); } manifestCp.append(entry.getPath().lastSegment()); // ear references aren't kept in the Maven Dependencies iter.remove(); continue; } // add non-dependency attribute // Check the scope & set WTP non-dependency as appropriate // Optional artifact shouldn't be deployed if (Artifact.SCOPE_PROVIDED.equals(scope) || Artifact.SCOPE_TEST.equals(scope) || Artifact.SCOPE_SYSTEM.equals(scope) || descriptor.isOptionalDependency()) { descriptor.addClasspathAttribute(NONDEPENDENCY_ATTRIBUTE); } // collect duplicate file names if (!names.add(entry.getPath().lastSegment())) { dups.add(entry.getPath().lastSegment()); } } String targetDir = mavenProject.getBuild().getDirectory(); // second pass disambiguates colliding entry file names iter = classpath.getEntryDescriptors().iterator(); while (iter.hasNext()) { IClasspathEntryDescriptor descriptor = iter.next(); IClasspathEntry entry = descriptor.getClasspathEntry(); if (dups.contains(entry.getPath().lastSegment())) { File src = new File(entry.getPath().toOSString()); String groupId = descriptor.getGroupId(); File dst = new File(targetDir, groupId + "-" + entry.getPath().lastSegment()); try { if (src.canRead()) { if (isDifferent(src, dst)) { // uses lastModified FileUtils.copyFile(src, dst); dst.setLastModified(src.lastModified()); } descriptor.setClasspathEntry( JavaCore.newLibraryEntry( Path.fromOSString(dst.getCanonicalPath()), // entry.getSourceAttachmentPath(), // entry.getSourceAttachmentRootPath(), // entry.getAccessRules(), // entry.getExtraAttributes(), // entry.isExported())); } } catch (IOException ex) { MavenLogger.log("File copy failed", ex); } } } if (opts.isSkinnyWar()) { // writing the manifest only works when the project has been properly created // placing this check on the top of the method broke 2 other tests // thats why its placed here now. if (ComponentCore.createComponent(project) == null) { return; } // write manifest, using internal API - seems ok for 3.4/3.5, though ArchiveManifest mf = J2EEProjectUtilities.readManifest(project); if (mf == null) { mf = new ArchiveManifestImpl(); } mf.addVersionIfNecessary(); mf.setClassPath(manifestCp.toString()); try { J2EEProjectUtilities.writeManifest(project, mf); } catch (Exception ex) { MavenLogger.log("Could not write web module manifest file", ex); } } }