diff --git a/cobigen-cli/cli-systemtest/src/test/java/com/devonfw/cobigen/cli/systemtest/AdaptTemplatesCommandIT.java b/cobigen-cli/cli-systemtest/src/test/java/com/devonfw/cobigen/cli/systemtest/AdaptTemplatesCommandIT.java index 2ec84fa49..77fcf2921 100644 --- a/cobigen-cli/cli-systemtest/src/test/java/com/devonfw/cobigen/cli/systemtest/AdaptTemplatesCommandIT.java +++ b/cobigen-cli/cli-systemtest/src/test/java/com/devonfw/cobigen/cli/systemtest/AdaptTemplatesCommandIT.java @@ -13,6 +13,7 @@ import com.devonfw.cobigen.api.constants.ConfigurationConstants; import com.devonfw.cobigen.api.util.TemplatesJarUtil; +import com.devonfw.cobigen.api.util.mavencoordinate.MavenCoordinateState; /** * Tests the usage of the adapt-templates command. @@ -25,7 +26,8 @@ public class AdaptTemplatesCommandIT extends AbstractCliTest { * @throws Exception test fails */ @Test - @Ignore // TODO: re-enable when template set adaptation is implemented + @Ignore + // TODO: re-enable when template set adaptation is implemented public void adaptTemplateSetTest() throws Exception { Path devTemplateSetPath = new File( @@ -89,8 +91,9 @@ public void adaptTemplateSetTest() throws Exception { * * @throws Exception test fails */ - @Ignore + @Test + @Ignore public void adaptTemplatesTest() throws Exception { Path cliSystemTestPath = new File( @@ -101,8 +104,13 @@ public void adaptTemplatesTest() throws Exception { if (!Files.exists(templatesPath)) { Files.createDirectories(templatesPath); } - TemplatesJarUtil.downloadJar("com.devonfw.cobigen", "templates-devon4j", "3.0.0", false, templatesPath.toFile()); - TemplatesJarUtil.downloadJar("com.devonfw.cobigen", "templates-devon4j", "3.0.0", true, templatesPath.toFile()); + + MavenCoordinateState nonSourcesJar = new MavenCoordinateState("com.devonfw.cobigen", "templates-devon4j", "3.0.0", + false); + MavenCoordinateState sourcesJar = new MavenCoordinateState("com.devonfw.cobigen", "templates-devon4j", "3.0.0", + true); + TemplatesJarUtil.downloadJar(nonSourcesJar, templatesPath.toFile()); + TemplatesJarUtil.downloadJar(sourcesJar, templatesPath.toFile()); String args[] = new String[2]; args[0] = "adapt-templates"; diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/TemplatesJarUtil.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/TemplatesJarUtil.java index 553720f69..029211094 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/TemplatesJarUtil.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/TemplatesJarUtil.java @@ -28,6 +28,7 @@ import com.devonfw.cobigen.api.constants.TemplatesJarConstants; import com.devonfw.cobigen.api.exception.CobiGenRuntimeException; import com.devonfw.cobigen.api.util.mavencoordinate.MavenCoordinate; +import com.devonfw.cobigen.api.util.mavencoordinate.MavenCoordinateState; /** * Utilities related to the templates jar. Includes the downloading, retrieval of the jar and the checkup of the @@ -39,24 +40,26 @@ public class TemplatesJarUtil { private static final Logger LOG = LoggerFactory.getLogger(TemplatesJarUtil.class); /** - * @param groupId of the artifact to download - * @param artifactId of the artifact to download - * @param version of the artifact to download - * @param isDownloadSource true if downloading source jar file + * Downloads a jar based on a MavenCoordinate and updates the path where the jar has been downloaded to. + * + * @param mavenCoordinateState holding the groupId, artifactId, version and whether the jar in question is a sources + * jar or not * @param templatesDirectory directory where the templates jar are located * @return fileName Name of the file downloaded */ - public static String downloadJar(String groupId, String artifactId, String version, boolean isDownloadSource, - File templatesDirectory) { + public static String downloadJar(MavenCoordinateState mavenCoordinateState, File templatesDirectory) { // By default the version should be latest - if (StringUtils.isEmpty(version)) { + if (StringUtils.isEmpty(mavenCoordinateState.getVersion())) { - version = "LATEST"; + mavenCoordinateState.setVersion("LATEST"); } String mavenUrl = "https://repository.sonatype.org/service/local/artifact/maven/" + "redirect?r=central-proxy&g=" - + groupId + "&a=" + artifactId + "&v=" + version; + + mavenCoordinateState.getGroupId() + "&a=" + mavenCoordinateState.getArtifactId() + "&v=" + + mavenCoordinateState.getVersion(); + + boolean isDownloadSource = mavenCoordinateState.isSource(); if (isDownloadSource) { mavenUrl = mavenUrl + "&c=sources"; @@ -64,6 +67,7 @@ public static String downloadJar(String groupId, String artifactId, String versi String fileName = ""; + // TODO: add another case with appropriate method for template sets Path jarFilePath = getJarFile(isDownloadSource, templatesDirectory.toPath()); try { if (jarFilePath == null || !Files.exists(jarFilePath) @@ -78,12 +82,15 @@ public static String downloadJar(String groupId, String artifactId, String versi if (!file.exists()) { Files.copy(inputStream, targetPath, StandardCopyOption.REPLACE_EXISTING); } + mavenCoordinateState.setMavenCoordinateLocalPath(targetPath); } conn.disconnect(); } else { fileName = jarFilePath.toFile().getPath() .substring(jarFilePath.toFile().getPath().lastIndexOf(File.separator) + 1); + mavenCoordinateState.setMavenCoordinateLocalPath(jarFilePath); } + mavenCoordinateState.setPresent(true); } catch (IOException e) { throw new CobiGenRuntimeException("Could not download file from " + mavenUrl, e); } @@ -99,8 +106,8 @@ public static String downloadJar(String groupId, String artifactId, String versi */ public static String downloadLatestDevon4jTemplates(boolean isDownloadSource, File templatesDirectory) { - return downloadJar(TemplatesJarConstants.DEVON4J_TEMPLATES_GROUPID, - TemplatesJarConstants.DEVON4J_TEMPLATES_ARTIFACTID, "LATEST", isDownloadSource, templatesDirectory); + return downloadJar(new MavenCoordinateState(TemplatesJarConstants.DEVON4J_TEMPLATES_GROUPID, + TemplatesJarConstants.DEVON4J_TEMPLATES_ARTIFACTID, "LATEST", isDownloadSource), templatesDirectory); } /** @@ -141,10 +148,8 @@ public static void downloadTemplatesByMavenCoordinates(Path templatesDirectory, } for (MavenCoordinate mavenCoordinate : mavenCoordinates) { - downloadJar(mavenCoordinate.getGroupId(), mavenCoordinate.getArtifactId(), mavenCoordinate.getVersion(), false, - downloaded.toFile()); - downloadJar(mavenCoordinate.getGroupId(), mavenCoordinate.getArtifactId(), mavenCoordinate.getVersion(), true, - downloaded.toFile()); + downloadJar(new MavenCoordinateState(mavenCoordinate, false), downloaded.toFile()); + downloadJar(new MavenCoordinateState(mavenCoordinate, true), downloaded.toFile()); } } @@ -274,7 +279,6 @@ private static HttpURLConnection initializeConnection(String mavenUrl) * * @return file of the jar downloaded or null if it was not found */ - // TODO: add check to validate template set jar pairs with default parameter for normal templates public static List getJarFiles(Path templatesDirectory) { ArrayList jarPaths = new ArrayList<>(); diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/mavencoordinate/MavenCoordinate.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/mavencoordinate/MavenCoordinate.java index 631a65007..b4d5a7a23 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/mavencoordinate/MavenCoordinate.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/mavencoordinate/MavenCoordinate.java @@ -13,6 +13,7 @@ * This MavenCoordinate class is just a dataholder with maven coordinates. */ public class MavenCoordinate { + /** * Constants needed for handling the template set jars */ @@ -111,6 +112,30 @@ public String getVersion() { return this.version; } + /** + * @param artifactId new value of {@link #getArtifactId}. + */ + public void setArtifactId(String artifactId) { + + this.artifactId = artifactId; + } + + /** + * @param groupId new value of {@link #getGroupId}. + */ + public void setGroupId(String groupId) { + + this.groupId = groupId; + } + + /** + * @param version new value of {@link #getVersion}. + */ + public void setVersion(String version) { + + this.version = version; + } + /** * Takes a string with multiple maven coordinates separates them and checks if they meet the maven naming conventions * and are therefore valid. diff --git a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/mavencoordinate/MavenCoordinateState.java b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/mavencoordinate/MavenCoordinateState.java index 7bec7e5db..f384959d8 100644 --- a/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/mavencoordinate/MavenCoordinateState.java +++ b/cobigen/cobigen-core-api/src/main/java/com/devonfw/cobigen/api/util/mavencoordinate/MavenCoordinateState.java @@ -114,6 +114,69 @@ public MavenCoordinateState(Path mavenCoordinatePath, String groupId, String art } + /** + * The constructor with a local path to a MavenCoordinate. By default all MavenCoordinates are neither present nor + * adapted. + * + * @param groupId the groupId of the maven artifact + * @param artifactId the artifactId of the maven artifact + * @param version the version of the maven artifact + * @param isSource whether the MavenCoordinate describes a source or not + */ + public MavenCoordinateState(String groupId, String artifactId, String version, boolean isSource) { + + super(groupId, artifactId, version); + + this.mavenCoordinateLocalPath = null; + this.isSource = isSource; + this.isPresent = false; // this field might be obsolete + this.isAdapted = false; + setToBeAdapted(false); + setValidMavenCoordinate(false); + + } + + /** + * The constructor with a local path to a MavenCoordinate. By default all MavenCoordinates are neither present nor + * adapted. This constructor converts a MavenCoordinate into a MavenCoordinateState. + * + * @param mavenCoordinatePath the local path to a MavenCoordinate + * @param mvnCoord an instance of a MavenCoordinate + * @param isSource whether the MavenCoordinate describes a source or not + */ + public MavenCoordinateState(Path mavenCoordinatePath, MavenCoordinate mvnCoord, boolean isSource) { + + super(mvnCoord.getGroupId(), mvnCoord.getArtifactId(), mvnCoord.getVersion()); + + this.mavenCoordinateLocalPath = mavenCoordinatePath; + this.isSource = isSource; + this.isPresent = false; // this field might be obsolete + this.isAdapted = false; + setToBeAdapted(false); + setValidMavenCoordinate(false); + + } + + /** + * The constructor with a local path to a MavenCoordinate. By default all MavenCoordinates are neither present nor + * adapted. This constructor converts a MavenCoordinate into a MavenCoordinateState. + * + * @param mvnCoord an instance of a MavenCoordinate + * @param isSource whether the MavenCoordinate describes a source or not + */ + public MavenCoordinateState(MavenCoordinate mvnCoord, boolean isSource) { + + super(mvnCoord.getGroupId(), mvnCoord.getArtifactId(), mvnCoord.getVersion()); + + this.mavenCoordinateLocalPath = null; + this.isSource = isSource; + this.isPresent = false; // this field might be obsolete + this.isAdapted = false; + setToBeAdapted(false); + setValidMavenCoordinate(false); + + } + /** * @return whether the MavenCoordinate is a source or not */ @@ -283,8 +346,6 @@ public static List getJarFilesToMavenCoordinateState(P } } - // TODO: templateset adapter anpassen sodas die neue templateset get jar file methode benutzt wird, wenn exception - // geworfen wurde /** * Creates a MavenCoordinateState object that exposes situational information.