Skip to content

Commit e85aab4

Browse files
1450 fix cli templates dependencies (#1460)
* #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 * #1450 fixed class paths cache file reading on Linux replaced codehaus.plexus.OS with new SystemUtil method added new getOS method to SystemUtils refactored registerPlugins and prependTemplatesClassloader added new addURLsFromCachedClassPathsFile method to MavenUtil added new generatePomFileHash method to MavenUtil fixed some typos * #1450 added a primitive int class variable to test maven project to trigger usage of lang3 ClassUtils
1 parent 48f365f commit e85aab4

File tree

5 files changed

+97
-65
lines changed

5 files changed

+97
-65
lines changed

cobigen-cli/cli-systemtest/src/test/resources/testdata/localmavenproject/maven.project/core/src/main/java/com/maven/project/sampledatamanagement/dataaccess/api/SampleDataEntity.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ public class SampleDataEntity implements SampleData {
2525
@Size(max = 30, min = 3)
2626
private String mail;
2727

28+
@Column(name = "PRIMITVEINT")
29+
private int primitiveInt;
30+
2831
private static final long serialVersionUID = 1L;
2932

3033
/**
@@ -115,4 +118,12 @@ public void setModificationCounter(int modificationCounter) {
115118

116119
}
117120

121+
public int getPrimitiveInt() {
122+
return this.primitiveInt;
123+
}
124+
125+
public void setPrimitiveInt(int primitiveInt) {
126+
this.primitiveInt = primitiveInt;
127+
}
128+
118129
}

cobigen-cli/cli/src/main/java/com/devonfw/cobigen/cli/utils/CobiGenUtils.java

Lines changed: 9 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,14 @@
33
import java.io.File;
44
import java.io.IOException;
55
import java.io.InputStream;
6-
import java.net.MalformedURLException;
7-
import java.net.URL;
86
import java.net.URLClassLoader;
97
import java.nio.file.Files;
108
import java.nio.file.Path;
119
import java.nio.file.Paths;
1210
import java.util.ArrayList;
1311
import java.util.Arrays;
1412
import java.util.List;
15-
import java.util.stream.Stream;
1613

17-
import org.codehaus.plexus.util.Os;
1814
import org.slf4j.Logger;
1915
import org.slf4j.LoggerFactory;
2016

@@ -31,7 +27,6 @@
3127
import com.devonfw.cobigen.impl.CobiGenFactory;
3228
import com.devonfw.cobigen.impl.extension.ClassServiceLoader;
3329
import com.google.common.base.Charsets;
34-
import com.google.common.hash.Hashing;
3530

3631
/**
3732
* Utilities class for CobiGen related operations. For instance, it creates a new CobiGen instance and registers all the
@@ -83,36 +78,17 @@ public static ClassLoader registerPlugins() {
8378

8479
Path rootCLIPath = getCliHomePath();
8580
File pomFile = extractArtificialPom();
86-
String pomFileHash;
87-
try {
88-
pomFileHash = com.google.common.io.Files.asByteSource(pomFile).hash(Hashing.murmur3_128()).toString();
89-
} catch (IOException e) {
90-
LOG.warn("Could not calculate hash of {}", pomFile.getAbsolutePath());
91-
pomFileHash = "";
92-
}
81+
82+
String pomFileHash = MavenUtil.generatePomFileHash(pomFile.toPath());
83+
9384
Path cpFile = rootCLIPath.resolve(String.format(MavenConstants.CLASSPATH_CACHE_FILE, pomFileHash));
9485

95-
if (!Files.exists(cpFile)) {
96-
MavenUtil.cacheMavenClassPath(pomFile.toPath(), cpFile);
97-
}
98-
// Read classPath.txt file and add to the class path all dependencies
99-
try (Stream<String> fileLinesStream = Files.lines(cpFile)) {
100-
URL[] classpathEntries = fileLinesStream
101-
.flatMap(e -> Arrays.stream(e.split(Os.isFamily(Os.FAMILY_WINDOWS) ? ";" : ":"))).map(path -> {
102-
try {
103-
return new File(path).toURI().toURL();
104-
} catch (MalformedURLException e) {
105-
LOG.error("URL of classpath entry {} is malformed", path, e);
106-
}
107-
return null;
108-
}).toArray(size -> new URL[size]);
109-
URLClassLoader cobigenClassLoader = new URLClassLoader(classpathEntries,
110-
Thread.currentThread().getContextClassLoader());
111-
ClassServiceLoader.lookupServices(cobigenClassLoader);
112-
return cobigenClassLoader;
113-
} catch (IOException e) {
114-
throw new CobiGenRuntimeException("Unable to read " + cpFile, e);
115-
}
86+
URLClassLoader cobigenClassLoader = MavenUtil.addURLsFromCachedClassPathsFile(cpFile, pomFile.toPath(),
87+
Thread.currentThread().getContextClassLoader());
88+
89+
ClassServiceLoader.lookupServices(cobigenClassLoader);
90+
return cobigenClassLoader;
91+
11692
}
11793

11894
/**

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

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
package com.devonfw.cobigen.api.util;
22

3+
import java.io.File;
34
import java.io.IOException;
5+
import java.net.MalformedURLException;
6+
import java.net.URL;
7+
import java.net.URLClassLoader;
48
import java.nio.file.Files;
59
import java.nio.file.Path;
610
import java.nio.file.Paths;
11+
import java.util.Arrays;
712
import java.util.List;
813
import java.util.concurrent.ExecutionException;
914
import java.util.concurrent.Future;
1015
import java.util.regex.Matcher;
1116
import java.util.regex.Pattern;
17+
import java.util.stream.Stream;
1218

1319
import org.apache.commons.lang3.StringUtils;
1420
import org.apache.commons.lang3.SystemUtils;
@@ -22,6 +28,8 @@
2228
import com.devonfw.cobigen.api.constants.MavenConstants;
2329
import com.devonfw.cobigen.api.exception.CobiGenRuntimeException;
2430
import com.google.common.collect.Lists;
31+
import com.google.common.hash.Hashing;
32+
import com.google.common.io.ByteSource;
2533

2634
/**
2735
* Utils to operate with maven artifacts
@@ -116,6 +124,61 @@ public static Path createCachedPomFromJar(Path pomFile, Path outputPath) {
116124
return cachedPomXml;
117125
}
118126

127+
/**
128+
* Adds URLs from class paths cache file to URLClassLoader. If no class paths cache file was found a new one will be
129+
* generated.
130+
*
131+
* @param classPathCacheFile the class paths cache file to read/create
132+
* @param pomFile POM file that defines the needed CobiGen dependencies to build
133+
* @param parentClassLoader parent ClassLoader
134+
*
135+
* @return URLClassLoader
136+
*/
137+
public static URLClassLoader addURLsFromCachedClassPathsFile(Path classPathCacheFile, Path pomFile,
138+
ClassLoader parentClassLoader) {
139+
140+
if (!Files.exists(classPathCacheFile)) {
141+
LOG.debug("Building class paths for maven configuration ...");
142+
cacheMavenClassPath(pomFile, classPathCacheFile);
143+
} else {
144+
LOG.debug("Taking cached class paths from: {}", classPathCacheFile);
145+
}
146+
147+
try (Stream<String> fileLinesStream = Files.lines(classPathCacheFile)) {
148+
URL[] classPathEntries = fileLinesStream
149+
.flatMap(e -> Arrays.stream(e.split(SystemUtil.getOS().contains("win") ? ";" : ":"))).map(path -> {
150+
try {
151+
return new File(path).toURI().toURL();
152+
} catch (MalformedURLException e) {
153+
LOG.error("URL of class path entry {} is malformed", path, e);
154+
}
155+
return null;
156+
}).toArray(size -> new URL[size]);
157+
158+
return new URLClassLoader(classPathEntries, parentClassLoader);
159+
} catch (IOException e) {
160+
throw new CobiGenRuntimeException("Unable to read " + classPathCacheFile, e);
161+
}
162+
}
163+
164+
/**
165+
* Generates a hash for the provided POM file
166+
*
167+
* @param pomFile to generate hash from
168+
* @return String generated hash
169+
*/
170+
public static String generatePomFileHash(Path pomFile) {
171+
172+
String pomFileHash;
173+
try {
174+
pomFileHash = ByteSource.wrap(Files.readAllBytes(pomFile)).hash(Hashing.murmur3_128()).toString();
175+
} catch (IOException e) {
176+
LOG.warn("Could not calculate hash of {}", pomFile.toUri());
177+
pomFileHash = "";
178+
}
179+
return pomFileHash;
180+
}
181+
119182
/**
120183
* @return the maven repository path
121184
*/

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ public class SystemUtil {
3838
*/
3939
public static final String LINE_SEPARATOR = java.lang.System.getProperty("line.separator");
4040

41-
/** Current Operating System, the code is exectued on */
41+
/** Current Operating System, the code is executed on */
4242
private static final String OS = System.getProperty("os.name").toLowerCase();
4343

44-
/** Maven exectuable */
44+
/** Maven executable */
4545
private static Path MVN_EXEC = null;
4646

4747
/**
@@ -188,6 +188,16 @@ public static Path convertUnixPathToWinOnWin(String path) {
188188
return returnVal;
189189
}
190190

191+
/**
192+
* Returns the Operating System type as a lower case string
193+
*
194+
* @return String Operating System type
195+
*/
196+
public static String getOS() {
197+
198+
return OS;
199+
}
200+
191201
/**
192202
* Determine mvn executable depending on the OS
193203
*

cobigen/cobigen-core/src/main/java/com/devonfw/cobigen/impl/generator/GenerationProcessorImpl.java

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import java.nio.file.Path;
1212
import java.nio.file.Paths;
1313
import java.nio.file.StandardCopyOption;
14-
import java.util.Arrays;
1514
import java.util.Collection;
1615
import java.util.Formatter;
1716
import java.util.List;
@@ -21,7 +20,6 @@
2120
import java.util.concurrent.ExecutionException;
2221
import java.util.concurrent.Future;
2322
import java.util.function.BiConsumer;
24-
import java.util.stream.Stream;
2523

2624
import org.apache.commons.io.FileUtils;
2725
import org.apache.commons.io.FilenameUtils;
@@ -65,8 +63,6 @@
6563
import com.devonfw.cobigen.impl.util.ConfigurationClassLoaderUtil;
6664
import com.devonfw.cobigen.impl.validator.InputValidator;
6765
import com.google.common.collect.Maps;
68-
import com.google.common.hash.Hashing;
69-
import com.google.common.io.ByteSource;
7066

7167
/**
7268
* Generation processor. Caches calculations and thus should be newly created on each request.
@@ -272,13 +268,7 @@ private ClassLoader prependTemplatesClassloader(Path configLocation, ClassLoader
272268
if (Files.exists(pomFile)) {
273269
LOG.debug("Found templates to be configured by maven.");
274270

275-
String pomFileHash;
276-
try {
277-
pomFileHash = ByteSource.wrap(Files.readAllBytes(pomFile)).hash(Hashing.murmur3_128()).toString();
278-
} catch (IOException e) {
279-
LOG.warn("Could not calculate hash of {}", pomFile.toUri());
280-
pomFileHash = "";
281-
}
271+
String pomFileHash = MavenUtil.generatePomFileHash(pomFile);
282272

283273
if (this.configurationHolder.isJarConfig()) {
284274
cpCacheFile = configLocation
@@ -287,25 +277,7 @@ private ClassLoader prependTemplatesClassloader(Path configLocation, ClassLoader
287277
cpCacheFile = configLocation.resolve(String.format(MavenConstants.CLASSPATH_CACHE_FILE, pomFileHash));
288278
}
289279

290-
if (!Files.exists(cpCacheFile)) {
291-
LOG.debug("Building classpath for maven templates configuration ...");
292-
MavenUtil.cacheMavenClassPath(pomFile, cpCacheFile);
293-
} else {
294-
LOG.debug("Taking cached classpath from {}", cpCacheFile);
295-
}
296-
297-
// Read classPath.txt file and add to the class path all dependencies
298-
try (Stream<String> fileLines = Files.lines(cpCacheFile)) {
299-
URL[] classpathEntries = fileLines.flatMap(e -> Arrays.stream(e.split(";"))).map(path -> {
300-
try {
301-
return new File(path).toURI().toURL();
302-
} catch (MalformedURLException e) {
303-
LOG.error("URL of classpath entry {} is malformed", path, e);
304-
}
305-
return null;
306-
}).toArray(size -> new URL[size]);
307-
combinedClassLoader = new URLClassLoader(classpathEntries, combinedClassLoader);
308-
}
280+
combinedClassLoader = MavenUtil.addURLsFromCachedClassPathsFile(cpCacheFile, pomFile, combinedClassLoader);
309281
}
310282

311283
// prepend jar/compiled resources as well

0 commit comments

Comments
 (0)