Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RHPAM-4970 locate pom with snapshot versioning on Springboot #277

Merged
merged 2 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.List;
import java.util.Optional;
import java.util.function.Predicate;
import java.util.regex.Pattern;

import org.apache.maven.model.Dependency;
import org.apache.maven.model.Model;
Expand All @@ -42,7 +43,7 @@
public abstract class AbstractFilesArtifactResolver extends ArtifactResolver {

private static final Logger log = LoggerFactory.getLogger(AbstractFilesArtifactResolver.class);

private final AFReleaseId releaseId;
private ClassLoader classLoader;
private List<URL> jarRepository;
private List<URL> effectivePoms;
Expand All @@ -53,15 +54,16 @@ public abstract class AbstractFilesArtifactResolver extends ArtifactResolver {
this.classLoader = classLoader;
this.jarRepository = new ArrayList<>();
this.effectivePoms = new ArrayList<>();
init(releaseId);
this.releaseId = releaseId;
init();
}

public boolean isLoaded() {
return pomParser != null;
}

// initialize in jar repository
private void init(AFReleaseId releaseId) {
protected void init() {
jarRepository = buildResources(name -> isInJarStructuredFolder(name, "jar"));
effectivePoms = buildResources(name -> isInJarStructuredFolder(name, "pom"));
pomParser = buildPomParser(releaseId);
Expand Down Expand Up @@ -99,7 +101,13 @@ private boolean isInJarStructuredFolder(String name, String type) {
}

private PomParser buildPomParser(AFReleaseId releaseId) {
List<URL> url = effectivePoms.stream().filter(e -> e.getFile().endsWith(toFile(releaseId, "pom"))).collect(toList());
final String pomName = toFile(releaseId, "pom");
List<URL> url = effectivePoms.stream().filter(e -> e.getFile().endsWith(pomName)).collect(toList());
if (url.isEmpty() && releaseId.getVersion().endsWith("-SNAPSHOT")) {
url = effectivePoms.stream().filter(e -> {
return Pattern.compile(toFileSnapshotRegex(releaseId, "pom")).matcher(e.getFile()).find();
}).collect(toList());
}
if (url.isEmpty()) {
return null;
}
Expand Down Expand Up @@ -171,6 +179,10 @@ private String toFile(AFReleaseId releaseId, String type) {
return releaseId.getArtifactId() + "-" + releaseId.getVersion() + "." + type;
}

private String toFileSnapshotRegex(AFReleaseId releaseId, String type) {
return releaseId.getArtifactId() + "-" + releaseId.getVersion().replace("-SNAPSHOT", ".*") + "." + type + "$";
}

@Override
public List<DependencyDescriptor> getArtifactDependecies(String artifactName) {
AFReleaseId releaseId = new AFReleaseIdImpl(artifactName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ public class ExplodedJarArtifactResolver extends AbstractFilesArtifactResolver {

private static final Logger log = LoggerFactory.getLogger(ExplodedJarArtifactResolver.class);


ExplodedJarArtifactResolver(ClassLoader classLoader, AFReleaseId releaseId) {
super(classLoader, releaseId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,8 @@ public class InJarArtifactResolver extends AbstractFilesArtifactResolver {

private static final Logger log = LoggerFactory.getLogger(InJarArtifactResolver.class);


InJarArtifactResolver(ClassLoader classLoader, AFReleaseId releaseId) {
super(classLoader, releaseId);

}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package org.appformer.maven.integration;

import junit.framework.TestCase;
import org.appformer.maven.support.AFReleaseId;
import org.appformer.maven.support.AFReleaseIdImpl;
import org.junit.Assert;
import org.mockito.Mockito;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.function.Predicate;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;

public class ArtifactResolverTest extends TestCase {

class MyInJarArtifactResolver extends InJarArtifactResolver {

MyInJarArtifactResolver(ClassLoader classLoader, AFReleaseId releaseId) {
super(classLoader, releaseId);
}

@Override
protected Optional<URL> tryInStructuredJar(AFReleaseId releaseId) {
return super.tryInStructuredJar(releaseId);
}
}

public void testInJarResolverSnapshotLocal() {
ClassLoader classLoader = getClass().getClassLoader();
AFReleaseId releaseId = new AFReleaseIdImpl("org.jbpm", "kjar", "1.0.0-SNAPSHOT", "jar");


ArrayList<URL> urlArrayList = new ArrayList<URL>();
urlArrayList.add(classLoader.getResource("BOOT-INF/classes/KIE-INF/lib/kjar-1.0.0-SNAPSHOT.pom"));

InJarArtifactResolver inJarArtifactResolver = new InJarArtifactResolver(classLoader, releaseId);
InJarArtifactResolver spy = Mockito.spy(inJarArtifactResolver);
doReturn(urlArrayList).when(spy).buildResources(any());

spy.init();

verify(spy).getURL(endsWith("BOOT-INF/classes/KIE-INF/lib/kjar-1.0.0-SNAPSHOT.pom"));
}

public void testInJarResolverSnapshotExternal() {
ClassLoader classLoader = getClass().getClassLoader();
AFReleaseId releaseId = new AFReleaseIdImpl("org.jbpm", "kjar", "1.0.0-SNAPSHOT", "jar");

InJarArtifactResolver inJarArtifactResolver = new InJarArtifactResolver(classLoader, releaseId );
InJarArtifactResolver spy = Mockito.spy(inJarArtifactResolver);

ArrayList<URL> urlArrayList = new ArrayList<URL>();
urlArrayList.add(classLoader.getResource("BOOT-INF/classes/KIE-INF/lib/kjar-1.0.0-20240717-143315-1.pom"));
doReturn(urlArrayList).when(spy).buildResources(any());

spy.init();

verify(spy).getURL(endsWith("BOOT-INF/classes/KIE-INF/lib/kjar-1.0.0-20240717-143315-1.pom"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<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/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>org.jbpm</groupId>
<artifactId>kjar</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging>

</project>

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<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/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>org.jbpm</groupId>
<artifactId>kjar</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging>

</project>

Loading