Skip to content

Commit

Permalink
#159: devon4j migration #160: AdoptOpenJDK #161: #162: exit codes (#163)
Browse files Browse the repository at this point in the history
* #159: devon4j migration improved/fixed
* #160: switched from OpenJDK to AdoptOpenJDK
* #161: fixed mvn invocation and exit code
* #162: propagate exit code
* #152: fix if installed software is reused (proper exit code so post installation quirks/enhancements are only once)
* #157: reuse local maven repo for integration tests to avoid waste
* #159: #157: improved integration test, create archetype and also test migration
* #18: archetype catalog should come with most recent archetype, updated and verify via test
  • Loading branch information
hohwille authored Aug 1, 2019
1 parent e7e9d6f commit b016b3e
Show file tree
Hide file tree
Showing 16 changed files with 290 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,33 +56,39 @@ public void migrate(File projectFolder, boolean singleStep) throws Exception {
* However, if that fails it may be provided manually.
* @param singleStep - {@code true} to only migrate to the next version, {@code false} otherwise (migrate to latest
* version).
* @throws Exception on error.
* @return the status code with {@code 0} for success and anything else for an error.
*/
public void migrate(File projectFolder, VersionIdentifier startVersion, boolean singleStep) throws Exception {
public int migrate(File projectFolder, VersionIdentifier startVersion, boolean singleStep) {

if (startVersion == null) {
startVersion = this.versionDetector.detectVersion(projectFolder);
try {
startVersion = this.versionDetector.detectVersion(projectFolder);
} catch (Exception e) {
Output.get().err("Failed to determine start version for migration!", e.getMessage());
e.printStackTrace();
return -1;
}
}
int migrations = 0;
VersionIdentifier version = startVersion;
while (true) {
MigrationStep step = this.fromVersion2MigrationStepMap.get(version);
if (step == null) {
complete(migrations, startVersion, version);
return;
return complete(migrations, startVersion, version);
} else {
try {
Output.get().banner("Migrating from version %s to %s ...", step.getFrom().toString(),
step.getTo().toString());
step.migrate(projectFolder);
if (singleStep) {
return;
return 0;
}
migrations++;
} catch (Exception e) {
Output.get().err("Migration from %s to %s failed: %s", step.getFrom().toString(), step.getTo().toString(),
e.getMessage());
e.printStackTrace();
return -1;
}
version = step.getTo();
}
Expand All @@ -94,15 +100,26 @@ public void migrate(File projectFolder, VersionIdentifier startVersion, boolean
* @param startVersion the initial {@link VersionIdentifier} before the migration.
* @param endVersion the final {@link VersionIdentifier} after the migration.
*/
private void complete(int migrations, VersionIdentifier startVersion, VersionIdentifier endVersion) {
private int complete(int migrations, VersionIdentifier startVersion, VersionIdentifier endVersion) {

if (migrations == 0) {
Output.get().warn("Project is already on version %s. No migrations available to update.",
startVersion.toString());
return 1;
} else {
Output.get().banner("Successfully applied %s migrations to migrate project from version %s to %s.",
Integer.toString(migrations), startVersion.toString(), endVersion.toString());
return 0;
}
}

/**
* @param start the {@link VersionIdentifier} to with the current version to start from.
* @return the {@link MigrationStep} to migrate to the next version.
*/
public MigrationStep get(VersionIdentifier start) {

return this.fromVersion2MigrationStepMap.get(start);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ public static MigrationImpl devon4j() {
.replaceProperty("flyway.version", "5.0.7") //
.replaceDependency(new VersionIdentifier("org.hibernate", "hibernate-validator", null),
new VersionIdentifier("org.hibernate.validator", "hibernate-validator", null))
.and() //
.java().replace("org.hibernate.Query", "org.hibernate.query.Query")
.addDependency(new VersionIdentifier("*-core", null),
new VersionIdentifier(VersionIdentifier.GROUP_ID_OASP4J_MODULES, "oasp4j-jpa", null))
.and().java() //
.replace("org.hibernate.Query", "org.hibernate.query.Query")
.replace("com.mysema.query.alias.Alias", "com.querydsl.core.alias.Alias")
.replace("com.mysema.query.jpa.impl.JPAQuery", "com.querydsl.jpa.impl.JPAQuery")
.replace("com.mysema.query.types.path.EntityPathBase", "com.querydsl.core.types.dsl.EntityPathBase")
Expand Down Expand Up @@ -111,6 +113,10 @@ public static MigrationImpl devon4j() {
"implements $1Dao, io.oasp.module.jpa.common.base.LegacyDaoQuerySupport<$1Entity>",
FileFilterPattern.reject("Application(MasterData)?DaoImpl\\.java")) //
.and() //
.next().to(VersionIdentifier.ofDevon4j("3.0.1")) //
.pom().replaceProperty("devon4j.version", "3.0.1").and() //
.next().to(VersionIdentifier.ofDevon4j("3.0.2")) //
.pom().replaceProperty("devon4j.version", "3.0.2").and() //
.next().to(VersionIdentifier.ofDevon4j("3.1.0")) //
.pom().replaceProperty("devon4j.version", "3.1.0") //
.replaceProperty("spring.boot.version", "2.1.6.RELEASE") //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ private int run(String[] args) throws Exception {
}
}
Log.init("migrator-" + migrationName, true);
migration.migrate(projectFolder, startVersion, singleStep);
return 0;
return migration.migrate(projectFolder, startVersion, singleStep);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ public FileMigration(Pattern namePattern) {
this.namePattern = namePattern;
}

/**
* @return the name {@link Pattern}.
*/
public Pattern getNamePattern() {

return this.namePattern;
}

@Override
public final void migrate(File file) throws Exception {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,30 @@ public MavenPropertyReplacement(String propertyName, String newValue, String new
this.newValue = newValue;
}

/**
* @return propertyName
*/
public String getPropertyName() {

return this.propertyName;
}

/**
* @return newPropertyName
*/
public String getNewPropertyName() {

return this.newPropertyName;
}

/**
* @return newValue
*/
public String getNewValue() {

return this.newValue;
}

@Override
public boolean migrateXml(Document xml) throws Exception {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package com.devonfw.tools.ide.migrator;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.assertj.core.api.Assertions;
import org.junit.Test;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import com.devonfw.tools.ide.migrator.file.FileMigration;
import com.devonfw.tools.ide.migrator.file.XmlFileMigration;
import com.devonfw.tools.ide.migrator.version.VersionIdentifier;
import com.devonfw.tools.ide.migrator.xml.MavenPropertyReplacement;
import com.devonfw.tools.ide.migrator.xml.XmlMigration;

/**
* Test of {@link Migrations}.
*/
public class MigrationsTest extends Assertions {

/**
* Test of {@link Migrations#devon4j()}.
*
* @throws Exception on error.
*/
@Test
public void testMigrations() throws Exception {

MigrationImpl devon4j = Migrations.devon4j();
List<VersionIdentifier> oasp4jVersions = Arrays.asList(VersionIdentifier.ofOasp4j("2.6.0"),
VersionIdentifier.ofOasp4j("2.6.1"), VersionIdentifier.ofOasp4j("3.0.0"));
List<VersionIdentifier> versions = new ArrayList<>(oasp4jVersions);
List<String> devon4jVersions = determineDevon4jVersions();
assertThat(devon4jVersions.size()).isGreaterThan(3);
String latestDevon4jVersion = devon4jVersions.get(devon4jVersions.size() - 1);
Path archetypeCatalog = Paths.get("../settings/src/main/settings/eclipse/archetype-catalog.xml");
List<String> archetypeCatalogLines = Files.readAllLines(archetypeCatalog, StandardCharsets.UTF_8);
assertThat(archetypeCatalogLines).contains(" <version>" + latestDevon4jVersion + "</version>");
for (String devon4jVersion : devon4jVersions) {
versions.add(VersionIdentifier.ofDevon4j(devon4jVersion));
}
MigrationStep step;
VersionIdentifier current = null;
int versionIndexMax = versions.size() - 1;
for (int versionIndex = 0; versionIndex <= versionIndexMax; versionIndex++) {
current = versions.get(versionIndex);
step = devon4j.get(current);
if (versionIndex < versionIndexMax) {
assertThat(step).isNotNull();
}
if (step != null) {
assertThat(step.getFrom()).isEqualTo(current);
assertThat(step.getTo()).isEqualTo(versions.get(versionIndex + 1));
List<FileMigration> fileMigrations = ((MigrationStepImpl) step).getFileMigrations();
assertThat(fileMigrations).isNotEmpty();
FileMigration versionUpgrade = fileMigrations.get(0);
assertThat(versionUpgrade.getNamePattern()).isSameAs(XmlFileMigration.POM_XML_PATTERN);
assertThat(versionUpgrade).isInstanceOf(XmlFileMigration.class);
List<XmlMigration> versionMigrations = ((XmlFileMigration) versionUpgrade).getMigrations();
assertThat(versionMigrations).isNotEmpty();
XmlMigration xmlMigration = versionMigrations.get(0);
assertThat(xmlMigration).isInstanceOf(MavenPropertyReplacement.class);
MavenPropertyReplacement mavenPropertyReplacement = (MavenPropertyReplacement) xmlMigration;
if (step.getFrom().getArtifactId().equals("devon4j")) {
assertThat(mavenPropertyReplacement.getPropertyName()).isEqualTo("devon4j.version");
} else {
assertThat(mavenPropertyReplacement.getPropertyName()).isEqualTo("oasp4j.version");
}
assertThat(mavenPropertyReplacement.getNewValue()).isEqualTo(step.getTo().getVersion());
}
}
}

private List<String> determineDevon4jVersions()
throws IOException, MalformedURLException, SAXException, ParserConfigurationException {

List<String> versions = new ArrayList<>();
String url = "https://repo1.maven.org/maven2/com/devonfw/java/modules/devon4j-basic/maven-metadata.xml";
InputStream in = new URL(url).openStream();
Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(in);
NodeList versionElementList = doc.getElementsByTagName("version");
for (int i = 0; i < versionElementList.getLength(); i++) {
Element element = (Element) versionElementList.item(i);
String version = element.getTextContent().trim();
if (!version.contains("-alpha") && !version.contains("-beta")) {
versions.add(version);
}
}
return versions;
}

}
8 changes: 4 additions & 4 deletions documentation/java.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ The arguments (`devon java «args»`) are explained by the following table:
.Usage of `devon java`
[options="header"]
|=======================
|*Argument(s)* |*Meaning*
|`setup` |setup OpenJDK (install and verify), link:configuration.asciidoc[configurable] via `JAVA_VERSION`
|`create` |create a new Java project based on https://github.com/devonfw/devon4j[devon4j].
|`update` [from «version»] [single] |migrate a https://github.com/devonfw/devon4j[devon4j] project to the latest version. If for some reasons the current devonfw version (e.g. oasp4j:2.6.0) can not be auto-detected you may provide it manually after the 'from' argument. Also the 'single' option allows to migrate only to the next available version."
|*Argument(s)* |*Meaning*
|`setup` |setup OpenJDK (install or update and verify), link:configuration.asciidoc[configurable] via `JAVA_VERSION` (e.g. `8u222b10` or `11.0.4_11`)
|`create` |create a new Java project based on https://github.com/devonfw/devon4j[devon4j].
|`migrate` [from «version»] [single] |migrate a https://github.com/devonfw/devon4j[devon4j] project to the latest version. If for some reasons the current devonfw version (e.g. oasp4j:2.6.0) can not be auto-detected you may provide it manually after the 'from' argument. Also the 'single' option allows to migrate only to the next available version."
|=======================
56 changes: 31 additions & 25 deletions scripts/src/main/resources/scripts/command/java
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,36 @@ function doSetup() {
fi
if [ -n "${1}" ] || [ ! -d "${JAVA_HOME}" ]
then
local software_version="${JAVA_VERSION:-11.0.2}"
local download_url="https://download.java.net/java/GA/jdk${software_version/\.*/}/9/GPL/openjdk-${software_version}"
local software_version="${JAVA_VERSION:-11.0.4_11}"
local major_version="${software_version/\.*/}"
major_version="${major_version/u*/}"
local jdk_folder="jdk-${software_version/_/%2B}"
if [ "${major_version}" = "8" ]
then
software_version="${software_version/_/b}"
local jdk_folder="jdk${software_version/b/-b}"
fi
local os
local extension=".tar.gz"
local software_dir="${JAVA_HOME}"
if doIsMacOs
then
os="mac"
software_dir="${DEVON_IDE_HOME}/software/jdk"
download_url="${download_url}_osx-x64_bin.tar.gz"
elif doIsWindows
then
download_url="${download_url}_windows-x64_bin.zip"
os="windows"
extension=".zip"
else
download_url="${download_url}_linux-x64_bin.tar.gz"
os="linux"
fi
local download_url="https://github.com/AdoptOpenJDK/openjdk${major_version}-binaries/releases/download/${jdk_folder}/OpenJDK${major_version}U-jdk_x64_${os}_hotspot_${software_version}${extension}"
doInstall "${software_dir}" "${download_url}" "java" "${software_version}"
if [ "${?}" = 0 ]
if [ ! -d "${JAVA_HOME}" ] && [ -d "${DEVON_IDE_HOME}/software/jdk" ] && [ "${software_dir}" = "${DEVON_IDE_HOME}/software/jdk" ]
then
if [ "${software_dir}" = "${DEVON_IDE_HOME}/software/jdk" ]
then
if [ ! -d "${JAVA_HOME}" ]
then
echo "Creating symlink as workaround for Java on MacOS"
doRunCommand "ln -s 'jdk/Contents/Home' '${JAVA_HOME}'"
fi
doExtendPath "${JAVA_HOME}"
fi
echo "Creating symlink as workaround for Java on MacOS"
doRunCommand "ln -s 'jdk/Contents/Home' '${JAVA_HOME}'"
doExtendPath "${JAVA_HOME}"
fi
fi
if [ -n "${1}" ]
Expand All @@ -44,8 +49,9 @@ function doSetup() {
}

#$@ args (see help)
function doUpdate() {
function doMigrate() {
doRunConfigurator com.devonfw.tools.ide.migrator.Migrator ${@}
exit ${?}
}

# CLI
Expand All @@ -54,13 +60,13 @@ then
echo "Install and manage Java."
echo
echo "Arguments:"
echo " setup setup Java (install and verify)"
echo " create «args» create new Java project based on devon4j template. If a single argument is provided,"
echo " this is the package name and is automatically split into groupId and artifactId."
echo " update [from «version»] [single] update the current devon4j project to the latest supported version. If for some reasons"
echo " the current devonfw version (e.g. oasp4j:2.6.0) can not be auto-detected you may provide"
echo " it manually after the 'from' argument. Also the 'single' option allows to migrate only to"
echo " the next available version."
echo " setup setup Java (install and verify)"
echo " create «args» create new Java project based on devon4j template. If a single argument is provided,"
echo " this is the package name and is automatically split into groupId and artifactId."
echo " migrate [from «version»] [single] update the current devon4j project to the latest supported version. If for some reasons"
echo " the current devonfw version (e.g. oasp4j:2.6.0) can not be auto-detected you may provide"
echo " it manually after the 'from' argument. Also the 'single' option allows to migrate only to"
echo " the next available version."
echo
echo "Options:"
elif [ -z "${1}" ] || [ "${1}" = "setup" ]
Expand All @@ -71,10 +77,10 @@ then
doSetup
shift
doMavenArchetype ${@}
elif [ "${1}" = "update" ]
elif [ "${1}" = "migrate" ]
then
shift
doUpdate ${@}
doMigrate ${@}
else
doFail "Unknown argument ${1}"
fi
Loading

0 comments on commit b016b3e

Please sign in to comment.