Skip to content

Commit

Permalink
4) Added maven-dependency-plugin:build-classpath and code to read res…
Browse files Browse the repository at this point in the history
…ulting files
  • Loading branch information
Fuud committed Aug 8, 2022
1 parent 7c8abae commit 50d2802
Show file tree
Hide file tree
Showing 7 changed files with 202 additions and 1 deletion.
Empty file added .top.project.dir
Empty file.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@
code [rev:2b8fcd50](https://github.com/Fuud/integration-tests-article/commit/2b8fcd509151ebc83c24d2b4e9fd0b665eb82ded)
2) Integration test module contains classpath from both
microservices [rev:cfbebf68](https://github.com/Fuud/integration-tests-article/commit/cfbebf68c0876dc2bfaaca8cb3074d7c6275d414)
3) added security to client service, but worker service is affected in tests (only!)
3) added security to client service, but worker service is affected in tests (
only!) [rev:7c8abae7](https://github.com/Fuud/integration-tests-article/commit/7c8abae738f827b7601bc42704c3c1e657ae09fb)
4) Added maven-dependency-plugin:build-classpath and code to read resulting files
40 changes: 40 additions & 0 deletions client-service/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,44 @@
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>com.coderplus.maven.plugins</groupId>
<artifactId>copy-rename-maven-plugin</artifactId>
<version>1.0</version>
<executions>
<execution>
<id>copy-pom</id>
<phase>process-classes</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<sourceFile>pom.xml</sourceFile>
<destinationFile>target/pom-copy.xml</destinationFile>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.3.0</version>
<executions>
<execution>
<id>generate classpath file for IT</id>
<goals>
<goal>build-classpath</goal>
</goals>
<phase>process-classes</phase>
<configuration>
<includeScope>runtime</includeScope>
<outputFile>${project.build.directory}/classpath_${project.artifactId}.txt</outputFile>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
92 changes: 92 additions & 0 deletions integration-tests/src/test/java/fuud/test/ClassPathHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package fuud.test;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.*;

public class ClassPathHelper {
private static Map<String, List<String>> artifactToClassPath = null;

public static synchronized List<String> getClasspathForArtifact(String artifactId) {
if (artifactToClassPath == null) {
artifactToClassPath = new HashMap<>();
try {
File topProjectDir = findTopProjectDir();
checkPomChanges(topProjectDir);
searchForClassPathFiles(topProjectDir, artifactToClassPath);
} catch (IOException e) {
throw new RuntimeException(e);
}
}

List<String> result = artifactToClassPath.get(artifactId);
if (result == null) {
throw new IllegalStateException("Classpath for artifactId " + artifactId + " is not found, known artifacts: " + artifactToClassPath.keySet());
}
return result;
}

private static void checkPomChanges(File topProjectDir) throws IOException {
File pomXml = new File(topProjectDir, "pom.xml");
if (pomXml.exists()) {
File targetPomFile = new File(new File(topProjectDir, "target"), "pom-copy.xml");
if (!targetPomFile.exists()) {
throw new IllegalStateException(targetPomFile.getAbsolutePath() + " is not generated, run `mvn process-classes` first");
}
if (!Files.readString(pomXml.toPath()).equals(Files.readString(targetPomFile.toPath()))) {
throw new IllegalStateException(targetPomFile.getAbsolutePath() + " is not equal to " + pomXml.getAbsolutePath() + ", run `mvn process-classes` first");
}
File[] probablySubmodules = topProjectDir.listFiles(File::isDirectory);
if (probablySubmodules != null) {
for (File probablySubmodule : probablySubmodules) {
checkPomChanges(probablySubmodule);
}
}
}
}

private static void searchForClassPathFiles(File topProjectDir, Map<String, List<String>> results) throws IOException {
File pomXml = new File(topProjectDir, "pom.xml");
if (pomXml.exists()) {
File targetDir = new File(topProjectDir, "target");
File[] classPathFiles = targetDir.listFiles(pathname -> pathname.getName().startsWith("classpath_") && pathname.getName().endsWith(".txt"));
if (classPathFiles != null) {
if (classPathFiles.length > 1) {
throw new IllegalStateException("Found more than one classpath file in dir " + targetDir.getAbsolutePath());
}
if (classPathFiles.length == 1) {
File classPathFile = classPathFiles[0];
List<String> classPath = new ArrayList<>(Arrays.asList(Files.readString(classPathFile.toPath()).split(System.getProperty("path.separator"))));
// maven-dependency-plugin build-classpath does not include module classes, let's include them now
classPath.add(0, new File(targetDir, "classes").getAbsolutePath());

String artifactId = classPathFile.getName().replaceAll("^classpath_", "").replaceAll(".txt$", "");
if (results.containsKey(artifactId)) {
throw new IllegalStateException("Duplicate artifact id: " + artifactId);
}
results.put(artifactId, classPath);
}
}
File[] probablySubmodules = topProjectDir.listFiles(File::isDirectory);
if (probablySubmodules != null) {
for (File probablySubmodule : probablySubmodules) {
searchForClassPathFiles(probablySubmodule, results);
}
}
}
}

private static File findTopProjectDir() throws IOException {
File topProjectDir = new File(".").getCanonicalFile();
do {
if (new File(topProjectDir, ".top.project.dir").exists()) {
return topProjectDir;
}
topProjectDir = topProjectDir.getParentFile();
} while (topProjectDir != null);

throw new IllegalStateException("Cannot find marker file .top.project.dir starting from " + new File(".").getAbsolutePath());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
public class TaskIntegrationTest {
@Test
public void testTaskSubmission() throws Exception {
System.out.println("Correct classpath for client app: " + ClassPathHelper.getClasspathForArtifact("client-service"));
System.out.println("Correct classpath for worker app: " + ClassPathHelper.getClasspathForArtifact("worker-service"));

ClientServiceApplication.main(new String[0]);
WorkerServiceApplication.main(new String[0]);

Expand Down
23 changes: 23 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,28 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<build>
<plugins>
<plugin>
<groupId>com.coderplus.maven.plugins</groupId>
<artifactId>copy-rename-maven-plugin</artifactId>
<version>1.0</version>
<executions>
<execution>
<id>copy-pom</id>
<phase>process-classes</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<sourceFile>pom.xml</sourceFile>
<destinationFile>target/pom-copy.xml</destinationFile>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>


</project>
41 changes: 41 additions & 0 deletions worker-service/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,45 @@
</dependency>
</dependencies>


<build>
<plugins>
<plugin>
<groupId>com.coderplus.maven.plugins</groupId>
<artifactId>copy-rename-maven-plugin</artifactId>
<version>1.0</version>
<executions>
<execution>
<id>copy-pom</id>
<phase>process-classes</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<sourceFile>pom.xml</sourceFile>
<destinationFile>target/pom-copy.xml</destinationFile>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.3.0</version>
<executions>
<execution>
<id>generate classpath file for IT</id>
<goals>
<goal>build-classpath</goal>
</goals>
<phase>process-classes</phase>
<configuration>
<includeScope>runtime</includeScope>
<outputFile>${project.build.directory}/classpath_${project.artifactId}.txt</outputFile>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

0 comments on commit 50d2802

Please sign in to comment.