Skip to content

Commit

Permalink
Use Maven API directly
Browse files Browse the repository at this point in the history
  • Loading branch information
mickaelistria committed Jun 29, 2022
1 parent 9fa1145 commit ca23f07
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 120 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ Require-Bundle: org.eclipse.core.runtime,
org.eclipse.cbi.p2repo.aggregator,
org.eclipse.cbi.p2repo.p2,
org.eclipse.cbi.p2repo.util,
org.eclipse.cbi.p2repo.p2.maven;visibility:=reexport
org.eclipse.m2e.maven.runtime,
org.eclipse.cbi.p2repo.p2.maven
Import-Package: org.eclipse.cbi.p2repo.p2.maven.util
Bundle-ActivationPolicy: lazy
Automatic-Module-Name: org.eclipse.cbi.p2repo.aggregator.engine.maven
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,21 @@

package org.eclipse.cbi.p2repo.aggregator.engine.maven;

import java.security.InvalidParameterException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

import org.apache.maven.model.Dependency;
import org.apache.maven.model.Model;
import org.apache.maven.model.Parent;
import org.eclipse.cbi.p2repo.aggregator.Aggregation;
import org.eclipse.cbi.p2repo.aggregator.AggregatorFactory;
import org.eclipse.cbi.p2repo.aggregator.Architecture;
Expand All @@ -31,16 +37,7 @@
import org.eclipse.cbi.p2repo.aggregator.VersionFormat;
import org.eclipse.cbi.p2repo.aggregator.WindowSystem;
import org.eclipse.cbi.p2repo.aggregator.util.InstallableUnitUtils;
import org.eclipse.cbi.p2repo.p2.maven.POM;
import org.eclipse.cbi.p2repo.p2.maven.pom.DependenciesType;
import org.eclipse.cbi.p2repo.p2.maven.pom.Dependency;
import org.eclipse.cbi.p2repo.p2.maven.pom.License;
import org.eclipse.cbi.p2repo.p2.maven.pom.LicensesType;
import org.eclipse.cbi.p2repo.p2.maven.pom.Model;
import org.eclipse.cbi.p2repo.p2.maven.pom.Parent;
import org.eclipse.cbi.p2repo.p2.maven.pom.PomFactory;
import org.eclipse.cbi.p2repo.p2.maven.util.VersionUtil;
import org.eclipse.cbi.p2repo.util.ExceptionUtils;
import org.eclipse.cbi.p2repo.util.LogUtils;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.emf.ecore.EObject;
Expand Down Expand Up @@ -196,92 +193,71 @@ public InstallableUnitMapping(VersionFormat versionFormat) {
this.versionFormat = versionFormat;
}

public POM asPOM() throws CoreException {
POM pom = new POM();
Model model = pom.getProject();
public org.apache.maven.model.Model asPOM() throws CoreException {
Model pom = new Model();
if(parent != null && !parent.isTransient()) {
Parent newParent = PomFactory.eINSTANCE.createParent();
Parent newParent = new Parent();
newParent.setGroupId(parent.map().getGroupId());
newParent.setArtifactId(parent.map().getArtifactId());
newParent.setVersion(parent.getVersionString(false, -1));
model.setParent(newParent);
pom.setParent(newParent);
}
pom.setModelVersion("4.0");
pom.setGroupId(map().getGroupId());
pom.setArtifactId(map().getArtifactId());
if(getMainArtifact() == null) {
pom.setPackaging("pom");
}

if(getVersion() != null && !getVersion().equals(Version.emptyVersion)) {
pom.setVersion(getVersionString(false, -1));
}
model.setModelVersion(POM.MODEL_VERSION);
model.setGroupId(map().getGroupId());
model.setArtifactId(map().getArtifactId());
if(getMainArtifact() == null)
model.setPackaging("pom");

if(getVersion() != null && !getVersion().equals(Version.emptyVersion))
model.setVersion(getVersionString(false, -1));

Collection<IRequirement> requirements = getRequirements();
if(requirements.size() > 0) {
DependenciesType dependencies = PomFactory.eINSTANCE.createDependenciesType();
boolean hasPlatformFragmentDependency = false;
for(IRequirement req : requirements) {
if(!(req instanceof IRequiredCapability))
continue;

IRequiredCapability cap = (IRequiredCapability) req;
// Only dependencies on IUs and OSGi bundles are considered in maven
if(IInstallableUnit.NAMESPACE_IU_ID.equals(cap.getNamespace()) ||
"osgi.bundle".equals(cap.getNamespace())) {
Dependency dependency = PomFactory.eINSTANCE.createDependency();

MavenItem dependencyMapping = map(cap.getName(), mappings);
dependency.setGroupId(dependencyMapping.getGroupId());
String artifactId = dependencyMapping.getArtifactId();
String generifiedId = generifyPlatformDependency(artifactId, req.getFilter());
if(generifiedId != null) {
if(hasPlatformFragmentDependency) {
continue; // don't add again
}
hasPlatformFragmentDependency = true;
artifactId = generifiedId;
}
dependency.setArtifactId(artifactId);
dependencies.getDependency().add(dependency);

if(cap.getRange() != null && !cap.getRange().equals(VersionRange.emptyRange)) {
boolean hasPlatformFragmentDependency[] = { false };
pom.setDependencies(getRequirements().stream() //
.filter(IRequiredCapability.class::isInstance) //
.map(IRequiredCapability.class::cast) //
// only consider require-bundle so far
.filter(req -> IInstallableUnit.NAMESPACE_IU_ID.equals(req.getNamespace())
|| "osgi.bundle".equals(req.getNamespace()))
.map(req -> {
Dependency dependency = new Dependency();
MavenItem dependencyMapping = map(req.getName(), mappings);
dependency.setGroupId(dependencyMapping.getGroupId());
String artifactId = dependencyMapping.getArtifactId();
String generifiedId = generifyPlatformDependency(artifactId, req.getFilter());
if(generifiedId != null) {
if(hasPlatformFragmentDependency[0]) {
return null; // don't add again
}
hasPlatformFragmentDependency[0] = true;
artifactId = generifiedId;
}
dependency.setArtifactId(artifactId);
if (req.getRange() != null && !req.getRange().equals(VersionRange.emptyRange)) {
StringBuilder versionRangeString = new StringBuilder();
Version low = cap.getRange().getMinimum();
Version high = cap.getRange().getMaximum();
if(cap.getRange().getIncludeMinimum() && Version.MAX_VERSION.equals(high)) {
versionRangeString.append("[").append(
getVersionStringForDependency(dependencyMapping, low)).append(",)");
}
else {
versionRangeString.append(
cap.getRange().getIncludeMinimum()
? '['
: '(');
Version low = req.getRange().getMinimum();
Version high = req.getRange().getMaximum();
if (req.getRange().getIncludeMinimum() && Version.MAX_VERSION.equals(high)) {
versionRangeString.append("[").append(getVersionStringForDependency(dependencyMapping, low))
.append(",)");
}
else {
versionRangeString.append(req.getRange().getIncludeMinimum() ? '[' : '(');
versionRangeString.append(getVersionStringForDependency(dependencyMapping, low));
versionRangeString.append(',');
versionRangeString.append(getVersionStringForDependency(dependencyMapping, high));
versionRangeString.append(
cap.getRange().getIncludeMaximum()
? ']'
: ')');
}
versionRangeString.append(req.getRange().getIncludeMaximum() ? ']' : ')');
}

dependency.setVersion(versionRangeString.toString());
}
else {
} else {
dependency.setVersion("[0.0,)");
}

if(cap.getMin() == 0)
dependency.setOptional(true);
}
}

// it is still possible that no dependency has been found since only dependencies on IUs and OSGi bundles
// are considered
// if no mavanizable dependency was found, don't create the dependencies section at all
if(dependencies.getDependency().size() > 0)
model.setDependencies(dependencies);
}
dependency.setOptional(req.getMin() == 0);
return dependency;
}).filter(Objects::nonNull) //
.collect(Collectors.toList()));

Map<String, String> iuProperties = new HashMap<String, String>(installableUnit.getProperties());
String name = extractProperty(iuProperties, IInstallableUnit.PROP_NAME);
Expand All @@ -299,48 +275,30 @@ public POM asPOM() throws CoreException {
if(description == null)
description = "";

model.setDescription(name + description);
pom.setDescription(name + description);
}

LicensesType licenses = PomFactory.eINSTANCE.createLicensesType();

Collection<ILicense> iuLicenses = installableUnit.getLicenses();
if(iuLicenses.size() > 0) {
for(ILicense iuLicense : iuLicenses) {
License license = PomFactory.eINSTANCE.createLicense();
boolean licenseSet = false;
if(iuLicense.getLocation() != null) {
licenseSet = true;
license.setUrl(iuLicense.getLocation().toString());
}
if(iuLicense.getBody() != null) {
licenseSet = true;
license.setComments(iuLicense.getBody());
List<org.apache.maven.model.License> mavenLicenses = new ArrayList<>();
installableUnit.getLicenses().stream() //
.filter(license -> license.getLocation() != null || license.getBody() != null).map(license -> {
org.apache.maven.model.License mavenLicense = new org.apache.maven.model.License();
if (license.getLocation() != null) {
mavenLicense.setUrl(license.getLocation().toString());
}
if(licenseSet)
licenses.getLicense().add(license);
}
}
mavenLicense.setComments(license.getBody());
return mavenLicense;
}).forEach(mavenLicenses::add);

ICopyright iuCopyright = installableUnit.getCopyright();
if(iuCopyright != null) {
License copyright = PomFactory.eINSTANCE.createLicense();
boolean copyrightSet = false;
if(iuCopyright != null && (iuCopyright.getLocation() != null || iuCopyright.getBody() != null)) {
org.apache.maven.model.License copyright = new org.apache.maven.model.License();
if(iuCopyright.getLocation() != null) {
copyrightSet = true;
copyright.setUrl(iuCopyright.getLocation().toString());
}
if(iuCopyright.getBody() != null) {
copyrightSet = true;
copyright.setComments(iuCopyright.getBody());
}
if(copyrightSet)
licenses.getLicense().add(copyright);
copyright.setComments(iuCopyright.getBody());
mavenLicenses.add(copyright);
}

if(licenses.getLicense().size() > 0)
model.setLicenses(licenses);

pom.setLicenses(mavenLicenses);
return pom;
}

Expand Down Expand Up @@ -643,7 +601,7 @@ public MavenItem map() throws CoreException {
return mapped;
}

private MavenItem map(String id, List<MavenMapping> mappings) throws CoreException {
private MavenItem map(String id, List<MavenMapping> mappings) throws InvalidParameterException {
MavenItem item = null;

for(MavenMapping mapping : mappings)
Expand All @@ -660,8 +618,8 @@ private MavenItem map(String id, List<MavenMapping> mappings) throws CoreExcepti
mappingDescriptor.append(mapping.toString());
}

throw ExceptionUtils.fromMessage(
"Unable to map IU to maven artifact: id=%s, mappings=%s", id, mappingDescriptor.toString());
throw new InvalidParameterException(String.format("Unable to map IU to maven artifact: id=%s, mappings=%s", id,
mappingDescriptor.toString()));
}

private boolean matchesFilter(IMatchExpression<IInstallableUnit> filter, Map<String, String> map) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

package org.eclipse.cbi.p2repo.aggregator.engine.maven;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
Expand All @@ -23,6 +24,7 @@
import java.util.List;
import java.util.Map;

import org.apache.maven.model.io.DefaultModelWriter;
import org.eclipse.cbi.p2repo.aggregator.Aggregation;
import org.eclipse.cbi.p2repo.aggregator.Contribution;
import org.eclipse.cbi.p2repo.aggregator.MavenItem;
Expand All @@ -42,6 +44,8 @@
import org.eclipse.cbi.p2repo.util.ExceptionUtils;
import org.eclipse.cbi.p2repo.util.LogUtils;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.ResourceSet;
Expand Down Expand Up @@ -297,7 +301,12 @@ private static void savePOMs(URI root, InstallableUnitMapping iu, URIConverter u
boolean isSource = iu.isSourceArtifact();
if(!isSource) {
URI pomUri = createPomURI(root, iu);
iu.asPOM().save(pomUri);
try {
new DefaultModelWriter().write(new File(java.net.URI.create(pomUri.toString())), Map.of(),
iu.asPOM());
} catch (IOException | CoreException e) {
throw new CoreException(new Status(IStatus.ERROR, MavenManager.class, e.getMessage(), e));
}
createCheckSum(pomUri, uriConverter, digests);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Require-Bundle: org.apache.ant,
org.kohsuke.args4j,
org.eclipse.osgi.services,
org.eclipse.equinox.p2.updatesite;bundle-version="1.1.0",
org.eclipse.equinox.p2.repository.tools
org.eclipse.equinox.p2.repository.tools,
org.eclipse.cbi.p2repo.p2.maven
Bundle-Vendor: %providerName
Automatic-Module-Name: org.eclipse.cbi.p2repo.aggregator.engine
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<unit id="org.eclipse.gmf.runtime.draw2d.ui.render.awt" version="0.0.0"/>
<unit id="org.eclipse.license.feature.group" version="0.0.0"/>
<unit id="org.eclipse.m2e.maven.indexer" version="0.0.0"/>
<unit id="org.eclipse.m2e.maven.runtime" version="0.0.0"/>
<unit id="org.eclipse.sdk.feature.group" version="0.0.0"/>
<unit id="org.eclipse.zest.core" version="0.0.0"/>
<unit id="org.kohsuke.args4j" version="0.0.0"/>
Expand Down

0 comments on commit ca23f07

Please sign in to comment.