Skip to content

Commit

Permalink
added mavencoordinates to downloadjar
Browse files Browse the repository at this point in the history
  • Loading branch information
quantumfate committed Mar 17, 2023
1 parent 0c57a02 commit c2b741f
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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(
Expand Down Expand Up @@ -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(
Expand All @@ -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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -39,31 +40,34 @@ 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";
}

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)
Expand All @@ -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);
}
Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -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());
}

}
Expand Down Expand Up @@ -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<Path> getJarFiles(Path templatesDirectory) {

ArrayList<Path> jarPaths = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -283,8 +346,6 @@ public static List<MavenCoordinateStatePair> 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.
Expand Down

0 comments on commit c2b741f

Please sign in to comment.