Skip to content

Commit b5e48e1

Browse files
#1450 added new integration test to CLI (#1459)
* #1450 added new integration test which uses a templates jar with a custom utility class and dependency * fixed #1450 refactored cached-pom.xml logic into new createCachedPomFromJar method added pom.xml to test templates project jar made sure that cached-pom.xml stays isolated in temporary folder * #1450 fixed extract jar error added exception trace to unable to extract exception
1 parent e24e4ef commit b5e48e1

File tree

9 files changed

+241
-20
lines changed

9 files changed

+241
-20
lines changed

cobigen-cli/cli-systemtest/pom.xml

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
<properties>
1414
<maven.test.path>src\test\resources\testdata\localmavenproject\maven.project</maven.test.path>
15+
<maven.test.templates.path>src\test\resources\testdata\templatesproject\templates-devon4j</maven.test.templates.path>
1516
</properties>
1617

1718
<dependencies>
@@ -79,20 +80,39 @@
7980
<goals>
8081
<goal>exec</goal>
8182
</goals>
83+
<configuration>
84+
<executable>mvn</executable>
85+
<workingDirectory>${maven.test.path}</workingDirectory>
86+
<arguments>
87+
<argument>install</argument>
88+
<!-- https://stackoverflow.com/a/66801171 -->
89+
<argument>-Djansi.force=true</argument>
90+
<argument>-Djansi.passthrough=true</argument>
91+
<argument>-B</argument>
92+
<argument>-Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn</argument>
93+
</arguments>
94+
</configuration>
95+
</execution>
96+
<execution>
97+
<id>Install test templates project</id>
98+
<phase>test-compile</phase>
99+
<goals>
100+
<goal>exec</goal>
101+
</goals>
102+
<configuration>
103+
<executable>mvn</executable>
104+
<workingDirectory>${maven.test.templates.path}</workingDirectory>
105+
<arguments>
106+
<argument>install</argument>
107+
<!-- https://stackoverflow.com/a/66801171 -->
108+
<argument>-Djansi.force=true</argument>
109+
<argument>-Djansi.passthrough=true</argument>
110+
<argument>-B</argument>
111+
<argument>-Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn</argument>
112+
</arguments>
113+
</configuration>
82114
</execution>
83115
</executions>
84-
<configuration>
85-
<executable>mvn</executable>
86-
<workingDirectory>${maven.test.path}</workingDirectory>
87-
<arguments>
88-
<argument>install</argument>
89-
<!-- https://stackoverflow.com/a/66801171 -->
90-
<argument>-Djansi.force=true</argument>
91-
<argument>-Djansi.passthrough=true</argument>
92-
<argument>-B</argument>
93-
<argument>-Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn</argument>
94-
</arguments>
95-
</configuration>
96116
</plugin>
97117
</plugins>
98118

cobigen-cli/cli-systemtest/src/test/java/com/devonfw/cobigen/cli/systemtest/GenerateCommandTest.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,34 @@ public void generateFromEntityTest() throws Exception {
6565
.exists();
6666
}
6767

68+
/**
69+
* Integration test of the generation from a templates jar using a utility class with an extra dependency. See:
70+
* https://github.com/devonfw/cobigen/issues/1450
71+
*
72+
* @throws Exception test fails
73+
*/
74+
@Test
75+
public void generateFromTemplatesJarWithUtilClassDependencyTest() throws Exception {
76+
77+
FileUtils.copyDirectory(new File(testFileRootPath + "templatesproject"), this.tmpProject.toFile());
78+
File baseProject = this.tmpProject.resolve("maven.project/core/").toFile();
79+
File templatesProject = this.tmpProject.resolve("templates-devon4j/target/templates-devon4j-dev-SNAPSHOT.jar")
80+
.toFile();
81+
82+
String args[] = new String[6];
83+
args[0] = "generate";
84+
args[1] = this.entityInputFile.getAbsolutePath();
85+
args[2] = "--increments";
86+
args[3] = "tos";
87+
args[4] = "-tp";
88+
args[5] = templatesProject.getAbsolutePath();
89+
90+
execute(args, false);
91+
92+
assertThat(baseProject.toPath().resolve("src/main/java/com/maven/project/sampledatamanagement/logic/api/to"))
93+
.exists();
94+
}
95+
6896
/**
6997
* Test with templates downloaded on demand
7098
*
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
3+
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
4+
<modelVersion>4.0.0</modelVersion>
5+
<groupId>com.devonfw.test</groupId>
6+
<artifactId>templates-devon4j</artifactId>
7+
<version>dev-SNAPSHOT</version>
8+
<packaging>jar</packaging>
9+
<properties>
10+
<maven.compiler.source>1.8</maven.compiler.source>
11+
<maven.compiler.target>1.8</maven.compiler.target>
12+
</properties>
13+
14+
<dependencies>
15+
<dependency>
16+
<groupId>org.apache.commons</groupId>
17+
<artifactId>commons-math3</artifactId>
18+
<version>3.6.1</version>
19+
</dependency>
20+
</dependencies>
21+
22+
<build>
23+
<resources>
24+
<resource>
25+
<directory>src/main/templates</directory>
26+
<targetPath>src/main/templates</targetPath>
27+
</resource>
28+
</resources>
29+
<plugins>
30+
<plugin>
31+
<groupId>org.codehaus.mojo</groupId>
32+
<artifactId>flatten-maven-plugin</artifactId>
33+
</plugin>
34+
<plugin>
35+
<groupId>org.apache.maven.plugins</groupId>
36+
<artifactId>maven-antrun-plugin</artifactId>
37+
<executions>
38+
<execution>
39+
<phase>prepare-package</phase>
40+
<goals>
41+
<goal>run</goal>
42+
</goals>
43+
<configuration>
44+
<tasks>
45+
<echo>Integrate POM manually</echo>
46+
<copy file="pom.xml" tofile="${project.build.outputDirectory}/pom.xml"/>
47+
</tasks>
48+
</configuration>
49+
</execution>
50+
</executions>
51+
</plugin>
52+
</plugins>
53+
</build>
54+
55+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package utils;
2+
3+
import org.apache.commons.math3.distribution.NormalDistribution;
4+
5+
public class MathUtils {
6+
7+
public double getNormalDistribution() {
8+
NormalDistribution normalDistribution = new NormalDistribution(10, 3);
9+
double randomValue = normalDistribution.sample();
10+
return randomValue;
11+
}
12+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<contextConfiguration xmlns="http://capgemini.com/devonfw/cobigen/ContextConfiguration" version="2.1">
3+
4+
<trigger id="crud_java_server_app" type="java" templateFolder="crud_java_server_app">
5+
<containerMatcher type="package" value="((.+\.)?([^\.]+))\.([^\.]+)\.dataaccess\.api"
6+
retrieveObjectsRecursively="false"/>
7+
<matcher type="fqn" value="((.+\.)?([^\.]+))\.([^\.]+)\.dataaccess\.api\.([^\.]+)Entity">
8+
<variableAssignment type="regex" key="rootPackage" value="1"/>
9+
<variableAssignment type="regex" key="domain" value="3"/>
10+
<variableAssignment type="regex" key="component" value="4"/>
11+
<variableAssignment type="regex" key="entityName" value="5"/>
12+
</matcher>
13+
</trigger>
14+
</contextConfiguration>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<templatesConfiguration xmlns="http://capgemini.com/devonfw/cobigen/TemplatesConfiguration"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.1">
4+
5+
<templates>
6+
<templateExtension ref="${variables.entityName}Eto.java" mergeStrategy="javamerge"/>
7+
</templates>
8+
9+
<templateScans>
10+
<templateScan templatePath="templates" destinationPath="src/main"/>
11+
</templateScans>
12+
13+
<increments>
14+
<increment name="tos" description="TO's">
15+
<templateRef ref="${variables.entityName}Eto.java"/>
16+
</increment>
17+
</increments>
18+
</templatesConfiguration>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package test;
2+
3+
class Test{
4+
private final double myVar = ${MathUtils.getNormalDistribution()};
5+
}

cobigen-cli/cli/src/main/java/com/devonfw/cobigen/cli/commands/GenerateCommand.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import static java.util.Map.Entry.comparingByValue;
44
import static java.util.stream.Collectors.toMap;
55

6+
import java.io.IOException;
7+
import java.net.URI;
68
import java.nio.charset.StandardCharsets;
79
import java.nio.file.Files;
810
import java.nio.file.Path;
@@ -34,6 +36,8 @@
3436
import com.devonfw.cobigen.cli.utils.CobiGenUtils;
3537
import com.devonfw.cobigen.cli.utils.ParsingUtils;
3638
import com.devonfw.cobigen.cli.utils.ValidationUtils;
39+
import com.devonfw.cobigen.impl.util.ConfigurationFinder;
40+
import com.devonfw.cobigen.impl.util.FileSystemUtil;
3741
import com.google.googlejavaformat.java.FormatterException;
3842

3943
import picocli.CommandLine.Command;
@@ -99,6 +103,8 @@ public Integer doAction() throws Exception {
99103
LOG.debug("Input files and output root path confirmed to be valid.");
100104
CobiGen cg = CobiGenUtils.initializeCobiGen(this.templatesProject);
101105

106+
resolveTemplateDependencies();
107+
102108
if (this.increments == null && this.templates != null) {
103109
Tuple<List<Object>, List<TemplateTo>> inputsAndArtifacts = preprocess(cg, TemplateTo.class);
104110
for (int i = 0; i < inputsAndArtifacts.getA().size(); i++) {
@@ -115,6 +121,31 @@ public Integer doAction() throws Exception {
115121
return 0;
116122
}
117123

124+
/**
125+
* Resolves dependencies from templates
126+
*
127+
* @throws IOException
128+
*/
129+
private void resolveTemplateDependencies() throws IOException {
130+
131+
Path templatesPath = null;
132+
Path pomFile = null;
133+
if (this.templatesProject != null) {
134+
templatesPath = FileSystemUtil.createFileSystemDependentPath(this.templatesProject.toUri());
135+
} else {
136+
URI findTemplatesLocation = ConfigurationFinder.findTemplatesLocation();
137+
templatesPath = FileSystemUtil.createFileSystemDependentPath(findTemplatesLocation);
138+
}
139+
140+
pomFile = templatesPath.resolve("pom.xml");
141+
142+
if (pomFile != null && Files.exists(pomFile)) {
143+
Path temporaryDirectory = Files.createDirectory(CobiGenUtils.getCliHomePath().resolve("temp"));
144+
temporaryDirectory.toFile().deleteOnExit();
145+
MavenUtil.resolveDependencies(pomFile, temporaryDirectory);
146+
}
147+
}
148+
118149
/**
119150
* For each input file it is going to get its matching templates or increments and then performs an intersection
120151
* between all of them, so that the user gets only the templates or increments that will work

cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/MavenUtil.java

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,7 @@ public static void cacheMavenClassPath(Path pomFile, Path cpFile) {
4646
List<String> args = Lists.newArrayList(SystemUtil.determineMvnPath().toString(), "dependency:build-classpath",
4747
"-Dmdep.outputFile=" + cpFile.toString());
4848
if (pomFile.getFileSystem().provider().getClass().getSimpleName().equals("ZipFileSystemProvider")) {
49-
Path cachedPomXml = cpFile.resolveSibling("cached-pom.xml");
50-
try {
51-
Files.copy(pomFile, cachedPomXml);
52-
} catch (IOException e) {
53-
throw new CobiGenRuntimeException("Unable to extract " + pomFile.toUri() + " from JAR to " + cachedPomXml);
54-
}
55-
pomFile = cachedPomXml;
56-
cachedPomXml.toFile().deleteOnExit();
49+
pomFile = createCachedPomFromJar(pomFile, cpFile.getParent());
5750
// just add this command in case your are working on jar
5851
// otherwise, pom resolution will fail if you work on a general maven module
5952
args.add("-f");
@@ -78,6 +71,51 @@ public static void resolveDependencies(Path mvnProjectRoot) {
7871
LOG.debug("Downloaded dependencies successfully.");
7972
}
8073

74+
/**
75+
* Resolve all maven dependencies of given pom.xml inside given project
76+
*
77+
* @param pomFile to cache and resolve from
78+
*
79+
* @param mvnProjectRoot the maven project root
80+
*/
81+
public static void resolveDependencies(Path pomFile, Path mvnProjectRoot) {
82+
83+
LOG.info(
84+
"Resolving maven dependencies for maven project {} to be able to make use of reflection in templates. Please be patient...",
85+
mvnProjectRoot);
86+
87+
if (pomFile.getFileSystem().provider().getClass().getSimpleName().equals("ZipFileSystemProvider")) {
88+
pomFile = createCachedPomFromJar(pomFile, mvnProjectRoot);
89+
}
90+
91+
List<String> args = Lists.newArrayList(SystemUtil.determineMvnPath().toString());
92+
args.add("-f");
93+
args.add(pomFile.toString());
94+
args.add("dependency:resolve");
95+
runCommand(mvnProjectRoot, args);
96+
LOG.debug("Downloaded dependencies successfully.");
97+
}
98+
99+
/**
100+
* Generates a cached-pom.xml file from a jar archive, automatically deletes the cached-pom.xml on exit
101+
*
102+
* @param pomFile to cache
103+
* @param outputPath output directory
104+
* @return the cached-pom.xml file
105+
*/
106+
public static Path createCachedPomFromJar(Path pomFile, Path outputPath) {
107+
108+
Path cachedPomXml = outputPath.resolve("cached-pom.xml");
109+
try {
110+
Files.copy(pomFile, cachedPomXml);
111+
} catch (IOException e) {
112+
throw new CobiGenRuntimeException("Unable to extract " + pomFile.toUri() + " from JAR to " + cachedPomXml, e);
113+
}
114+
pomFile = cachedPomXml;
115+
cachedPomXml.toFile().deleteOnExit();
116+
return cachedPomXml;
117+
}
118+
81119
/**
82120
* @return the maven repository path
83121
*/

0 commit comments

Comments
 (0)