Skip to content

Commit

Permalink
Merge pull request #63 from gdgib/G2-1559-MavenModuleProfile
Browse files Browse the repository at this point in the history
G2-1559 Add module & profile support for MavenPOM
  • Loading branch information
gdgib authored Apr 21, 2024
2 parents c699793 + 9ece32a commit 7e532ed
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@
@Getter(AccessLevel.PROTECTED)
@Slf4j
public class MavenRepository extends ARepository<MavenCoordinates, MavenSystem> {
protected static MavenCoordinates validate(MavenCoordinates coordinates) {
if ((coordinates.getGroupId() == null) || (coordinates.getArtifactId() == null) || (coordinates.getVersion() == null)) throw new NullPointerException(MavenCoordinatesDescriber.create().describe(coordinates).getName());
return coordinates;
}

@ToString.Exclude
@EqualsAndHashCode.Exclude
@Getter(AccessLevel.PUBLIC)
Expand Down Expand Up @@ -209,7 +214,7 @@ public Artifact<MavenCoordinates> load(MavenCoordinates coordinates, IReassertGr
{
final Path resolvedPath = getResolvedCacheArea().apply(unpackaged);
final MavenPOM resolved = readPom(resolvedPath);

final Collection<ILicenseApplied> licenses = getLicenses(resolved);
for (ILicenseApplied license : licenses) {
builder.vertex(license).edge(artifact, license, new Notice());
Expand Down Expand Up @@ -297,9 +302,4 @@ protected Path resolve(MavenCoordinates coordinates, Path path) {

return path;
}

protected static MavenCoordinates validate(MavenCoordinates coordinates) {
if ((coordinates.getGroupId() == null) || (coordinates.getArtifactId() == null) || (coordinates.getVersion() == null)) throw new NullPointerException(MavenCoordinatesDescriber.create().describe(coordinates).getName());
return coordinates;
}
}
15 changes: 15 additions & 0 deletions re-maven/src/main/java/com/g2forge/reassert/maven/MavenSystem.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.g2forge.reassert.maven;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand All @@ -15,10 +17,14 @@
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.module.paranamer.ParanamerModule;
import com.g2forge.alexandria.java.core.enums.HEnum;
import com.g2forge.alexandria.java.io.RuntimeIOException;
import com.g2forge.alexandria.java.io.dataaccess.IDataSource;
import com.g2forge.alexandria.java.type.ref.ITypeRef;
import com.g2forge.gearbox.maven.MavenPackaging;
import com.g2forge.reassert.core.api.described.IDescriber;
import com.g2forge.reassert.core.api.module.IContext;
import com.g2forge.reassert.core.api.system.ISystem;
import com.g2forge.reassert.maven.model.MavenPOM;
import com.g2forge.reassert.maven.model.convert.MavenXmlModule;

import lombok.AccessLevel;
Expand Down Expand Up @@ -74,6 +80,15 @@ public IDescriber<MavenCoordinates> getCoordinateDescriber() {
return MavenCoordinatesDescriber.create();
}

public MavenPOM parse(IDataSource source) {
final XmlMapper mapper = getMapper();
try (final InputStream stream = source.getStream(ITypeRef.of(InputStream.class))) {
return mapper.readValue(stream, MavenPOM.class);
} catch (IOException e) {
throw new RuntimeIOException("Failed to parse Maven POM from " + source, e);
}
}

public MavenCoordinates parse(String string) {
final Matcher matcher = Pattern.compile("([^:]+):([^:]+):([^:]+)(:([^:]+))?").matcher(string);
if (!matcher.matches()) throw new IllegalArgumentException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,15 @@ public class MavenPOM implements IDescriptor {
@Singular
protected final Map<String, String> properties;

@Singular
protected final List<String> modules;

@Singular
protected final List<MavenProfile> profiles;

@JsonCreator
public MavenPOM(String groupId, String artifactId, String version, MavenPackaging packaging, MavenParent parent, List<MavenLicense> licenses, List<MavenDependency> dependencies, Map<String, String> properties) {
this(new MavenCoordinates(null, groupId, artifactId, version, packaging), parent, licenses, dependencies, properties);
public MavenPOM(String groupId, String artifactId, String version, MavenPackaging packaging, MavenParent parent, List<MavenLicense> licenses, List<MavenDependency> dependencies, Map<String, String> properties, List<String> modules, List<MavenProfile> profiles) {
this(new MavenCoordinates(null, groupId, artifactId, version, packaging), parent, licenses, dependencies, properties, modules, profiles);
}

@JsonIgnore
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.g2forge.reassert.maven.model;

import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

import lombok.Builder;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import lombok.Singular;

@Data
@Builder(toBuilder = true)
@RequiredArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
public class MavenProfile {
protected final String id;

@Singular
protected final List<MavenDependency> dependencies;

@Singular
protected final Map<String, String> properties;

@Singular
protected final List<String> modules;
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.g2forge.reassert.core.test.ATestRepository;
import com.g2forge.reassert.maven.model.MavenLicense;
import com.g2forge.reassert.maven.model.MavenPOM;
import com.g2forge.reassert.maven.model.MavenProfile;
import com.g2forge.reassert.standard.model.contract.license.FamilyVersionLicense;
import com.g2forge.reassert.standard.model.contract.license.StandardLicenseFamily;

Expand Down Expand Up @@ -63,7 +64,7 @@ public void pom() {
final Graph<IVertex, IEdge> graph = getGraph();
final Artifact<MavenCoordinates> artifact = HReassertModel.findArtifact(graph, getCoordinates());
final Collection<MavenPOM> poms = HReassertModel.get(graph, artifact, false, Describes.class::isInstance, ITypeRef.of(MavenPOM.class)).stream().map(pom -> pom.toBuilder().clearDependencies().clearProperties().build()).collect(Collectors.toList());
HAssert.assertEquals(HCollection.asList(MavenPOM.builder().coordinates(getCoordinates().toBuilder().packaging(MavenPackaging.POM).build()).license(new MavenLicense("The Apache License, Version 2.0", "https://github.com/${alexandria.organization}/${alexandria.repository}/blob/${project.version}/LICENSE")).build()), poms);
HAssert.assertEquals(HCollection.asList(MavenPOM.builder().coordinates(getCoordinates().toBuilder().packaging(MavenPackaging.POM).build()).license(new MavenLicense("The Apache License, Version 2.0", "https://github.com/${alexandria.organization}/${alexandria.repository}/blob/${project.version}/LICENSE")).profile(new MavenProfile("release", null, null, null)).build()), poms);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.g2forge.reassert.maven.model;

import org.junit.Test;

import com.g2forge.alexandria.java.core.helpers.HCollection;
import com.g2forge.alexandria.java.core.resource.Resource;
import com.g2forge.alexandria.java.io.dataaccess.ResourceDataSource;
import com.g2forge.alexandria.test.HAssert;
import com.g2forge.reassert.core.api.module.Context;
import com.g2forge.reassert.maven.MavenCoordinates;
import com.g2forge.reassert.maven.MavenSystem;

public class TestMavenPOM {
@Test
public void test() {
final MavenSystem mavenSystem = new MavenSystem(Context.getContext());
final MavenPOM actual = mavenSystem.parse(new ResourceDataSource(new Resource(getClass(), "test-pom.xml.txt")));

HAssert.assertEquals(new MavenCoordinates(mavenSystem, "Group", "Artifact", "Version", null), actual.getCoordinates());
HAssert.assertNull(actual.getParent());
HAssert.assertNull(actual.getLicenses());
HAssert.assertNull(actual.getProperties());
HAssert.assertEquals(HCollection.asList(new MavenDependency(new MavenCoordinates(mavenSystem, "Dependencies", "One", "1.0.0", null), null, false), new MavenDependency(new MavenCoordinates(mavenSystem, "Dependencies", "Two", "2.0.0", null), MavenScope.Test, false)), actual.getDependencies());
HAssert.assertEquals(HCollection.asList("ModuleA", "ModuleB"), actual.getModules());
HAssert.assertEquals(HCollection.asList(new MavenProfile("Profile1", null, null, HCollection.asList("ModuleC"))), actual.getProfiles());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?xml version='1.0' encoding='UTF-8'?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>Group</groupId>
<artifactId>Artifact</artifactId>
<version>Version</version>

<dependencies>
<dependency>
<groupId>Dependencies</groupId>
<artifactId>One</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>Dependencies</groupId>
<artifactId>Two</artifactId>
<version>2.0.0</version>
<scope>test</scope>
</dependency>
</dependencies>

<modules>
<module>ModuleA</module>
<module>ModuleB</module>
</modules>

<profiles>
<profile>
<id>Profile1</id>
<modules>
<module>ModuleC</module>
</modules>
</profile>
</profiles>
</project>

0 comments on commit 7e532ed

Please sign in to comment.