Skip to content

Commit

Permalink
Merge pull request #100 from jGauravGupta/FISH-9510
Browse files Browse the repository at this point in the history
FISH-9510 Payara Micro Gradle plugin auto select default latest version Payara Micro based on Jakarta EE API version
  • Loading branch information
jGauravGupta authored Nov 1, 2024
2 parents 1f691ca + 2c80914 commit c2bc023
Show file tree
Hide file tree
Showing 9 changed files with 282 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pipeline {
sh '''
ls -lrt
cd payara-micro-gradle-plugin
gradle clean build -x check
gradle clean build
'''
echo '*#*#*#*#*#*#*#*#*#*#*#*# Built SRC *#*#*#*#*#*#*#*#*#*#*#*#*#*#*#'
}
Expand Down
4 changes: 3 additions & 1 deletion payara-micro-gradle-plugin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ plugins {
}

group = 'fish.payara.gradle.plugins'
version = '2.0.1'
version = '2.0.2-SNAPSHOT'

sourceCompatibility = '1.8'
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'
Expand All @@ -20,7 +20,9 @@ repositories {
dependencies {
api gradleApi()
api localGroovy()
implementation 'org.jdom:jdom2:2.0.6'
implementation 'commons-io:commons-io:2.11.0'
implementation 'org.apache.httpcomponents:httpclient:4.5.14'
implementation 'org.apache.commons:commons-lang3:3.12.0'
implementation 'org.apache.commons:commons-text:1.10.0'
testImplementation gradleTestKit()
Expand Down
2 changes: 2 additions & 0 deletions payara-micro-gradle-plugin/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
action.custom-1=publishToMavenLocal
action.custom-1.args=--configure-on-demand -w -x check publishToMavenLocal
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
*
* Copyright (c) [2018-2021] Payara Foundation and/or its affiliates. All rights reserved.
* Copyright (c) [2018-2024] Payara Foundation and/or its affiliates. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
Expand Down Expand Up @@ -44,7 +44,7 @@ public interface Configuration {
String MICRO_THREAD_NAME = "PayaraMicroThread";
String MICRO_READY_MESSAGE = "ready in";

String DEFAULT_MICRO_VERSION = "6.2023.1";
String DEFAULT_MICRO_VERSION = "6.2024.10";

String JAR_EXTENSION = "jar";
String WAR_EXTENSION = "war";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
*
* Copyright (c) 2018-2020 Payara Foundation and/or its affiliates. All rights reserved.
* Copyright (c) 2018-2024 Payara Foundation and/or its affiliates. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
Expand Down Expand Up @@ -41,6 +41,7 @@
import static fish.payara.gradle.plugins.micro.Configuration.DEFAULT_MICRO_VERSION;
import static fish.payara.gradle.plugins.micro.PayaraMicroPlugin.PLUGIN_ID;
import java.util.Map;
import org.gradle.api.GradleException;
import org.gradle.api.Project;

public class PayaraMicroExtension {
Expand Down Expand Up @@ -72,7 +73,7 @@ public class PayaraMicroExtension {

private String payaraMicroAbsolutePath;

private String payaraVersion = DEFAULT_MICRO_VERSION;
private String payaraVersion;

private String contextRoot;

Expand Down Expand Up @@ -179,7 +180,20 @@ public void setPayaraMicroAbsolutePath(String payaraMicroAbsolutePath) {
}

public String getPayaraVersion() {
return getValue("payaraVersion", payaraVersion);
if(payaraVersion != null) {
return payaraVersion;
}
PayaraMicroVersionSelector selector = new PayaraMicroVersionSelector(project);
String version = null;
try {
version = selector.fetchPayaraVersion();
} catch (GradleException ex) {
project.getLogger().error("Unable to fetch the version from Maven Central " + ex.toString());
}
if (version == null) {
version = DEFAULT_MICRO_VERSION;
}
return getValue("payaraVersion", version);
}

public void setPayaraVersion(String payaraVersion) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
/*
*
* Copyright (c) [2024] Payara Foundation and/or its affiliates. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
* and Distribution License("CDDL") (collectively, the "License"). You
* may not use this file except in compliance with the License. You can
* obtain a copy of the License at
* https://github.com/payara/Payara/blob/master/LICENSE.txt
* See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each
* file and include the License file at glassfish/legal/LICENSE.txt.
*
* GPL Classpath Exception:
* The Payara Foundation designates this particular file as subject to the "Classpath"
* exception as provided by the Payara Foundation in the GPL Version 2 section of the License
* file that accompanied this code.
*
* Modifications:
* If applicable, add the following below the License Header, with the fields
* enclosed by brackets [] replaced by your own identifying information:
* "Portions Copyright [year] [name of copyright owner]"
*
* Contributor(s):
* If you wish your version of this file to be governed by only the CDDL or
* only the GPL Version 2, indicate your decision by adding "[Contributor]
* elects to include this software in this distribution under the [CDDL or GPL
* Version 2] license." If you don't indicate a single choice of license, a
* recipient has the option to distribute your version of this file under
* either the CDDL, the GPL Version 2 or to extend the choice of license to
* its licensees as provided above. However, if you add GPL Version 2 code
* and therefore, elected the GPL Version 2 license, then the option applies
* only if the new code is made subject to such option by the copyright
* holder.
*/
package fish.payara.gradle.plugins.micro;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.gradle.api.GradleException;
import org.gradle.api.Project;
import org.gradle.api.artifacts.Dependency;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.input.SAXBuilder;

import java.io.StringReader;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Predicate;
import org.gradle.api.artifacts.DependencySet;

public class PayaraMicroVersionSelector {

private static final String MAVEN_METADATA_URL = "https://repo1.maven.org/maven2/fish/payara/extras/payara-micro/maven-metadata.xml";
private static final String GROUP_ID_JAKARTA = "jakarta.platform";
private static final String GROUP_ID_JAVAX = "javax";
private static final String ARTIFACT_ID_JAVAEE_WEB_API = "javaee-web-api";
private static final String ARTIFACT_ID_JAVAEE_API = "javaee-api";

private static final Map<Integer, String> JAKARTA_TO_PAYARA_MAP = new HashMap<>();
private final Project project;

public PayaraMicroVersionSelector(Project project) {
this.project = project;
}

static {
JAKARTA_TO_PAYARA_MAP.put(11, "7."); // Jakarta EE 11 -> Payara Micro 7.x
JAKARTA_TO_PAYARA_MAP.put(10, "6."); // Jakarta EE 10 -> Payara Micro 6.x
JAKARTA_TO_PAYARA_MAP.put(9, "6."); // Jakarta EE 9 -> Payara Micro 6.x
JAKARTA_TO_PAYARA_MAP.put(8, "5."); // Jakarta EE 8 -> Payara Micro 5.x
}

public String fetchPayaraVersion() throws GradleException {
String jakartaVersion = getJakartaVersion();
String payaraMicroVersion = null;
if (jakartaVersion != null) {
int jakartaMajorVersion = getJakartaMajorVersion(jakartaVersion);
if (jakartaMajorVersion != -1) {
payaraMicroVersion = getPayaraMicroVersion(jakartaMajorVersion);
if (payaraMicroVersion != null) {
project.getLogger().info("Selected Payara Micro version '" + payaraMicroVersion + "' for Jakarta EE version '" + jakartaVersion + "'.");
} else {
project.getLogger().warn("Could not determine the appropriate Payara Micro version.");
}
} else {
project.getLogger().warn("Invalid Jakarta EE version: " + jakartaVersion);
}
} else {
payaraMicroVersion = getLatestPayaraMicroVersion();
if (payaraMicroVersion != null) {
project.getLogger().info("No Jakarta EE dependency found. Using latest Payara Micro version: " + payaraMicroVersion);
} else {
project.getLogger().warn("Could not fetch the latest Payara Micro version.");
}
}
return payaraMicroVersion;
}

private String getJakartaVersion() {
DependencySet dependencies = project.getConfigurations().getByName("compileClasspath").getAllDependencies();
for (Dependency dependency : dependencies) {
if (GROUP_ID_JAKARTA.equals(dependency.getGroup())) {
return dependency.getVersion();
}
if (GROUP_ID_JAVAX.equals(dependency.getGroup())
&& (ARTIFACT_ID_JAVAEE_API.equals(dependency.getName()) || ARTIFACT_ID_JAVAEE_WEB_API.equals(dependency.getName()))) {
return dependency.getVersion();
}
}
return null;
}

private int getJakartaMajorVersion(String jakartaVersion) {
try {
return Integer.parseInt(jakartaVersion.split("\\.")[0]);
} catch (NumberFormatException e) {
return -1;
}
}

public static String getPayaraMicroVersion(int jakartaMajorVersion) throws GradleException {
return fetchPayaraMicroVersion(version -> version.startsWith(JAKARTA_TO_PAYARA_MAP.get(jakartaMajorVersion)));
}

private String getLatestPayaraMicroVersion() throws GradleException {
return fetchPayaraMicroVersion(version -> true);
}

private static String fetchPayaraMicroVersion(Predicate<String> versionPredicate) throws GradleException {
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
HttpGet httpGet = new HttpGet(MAVEN_METADATA_URL);
try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
HttpEntity entity = response.getEntity();

if (entity != null) {
String xmlContent = EntityUtils.toString(entity);
SAXBuilder saxBuilder = new SAXBuilder();
Document document = saxBuilder.build(new StringReader(xmlContent));
Element rootElement = document.getRootElement();
Element versionsElement = rootElement.getChild("versioning").getChild("versions");

if (versionsElement != null) {
List<Element> versionElements = versionsElement.getChildren("version");

for (int i = versionElements.size() - 1; i >= 0; i--) {
String version = versionElements.get(i).getText();
if (versionPredicate.test(version)) {
return version;
}
}
}
}
}
} catch (Exception e) {
throw new GradleException("Error fetching Payara Micro version", e);
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ public class BundleLifecycleTest extends BaseTest {
public void uberJarBundleTest() throws Exception {

buildProject();
buildExtension();
PayaraMicroExtension extension = buildExtension();
int jakataEEVersion = JakartaEEVersionSelector.getMajorJakartaEEVersion();
String payaraVersion = PayaraMicroVersionSelector.getPayaraMicroVersion(jakataEEVersion);
extension.setPayaraVersion(payaraVersion);
bundleMicro();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
*
* Copyright (c) 2024 Payara Foundation and/or its affiliates. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
* and Distribution License("CDDL") (collectively, the "License"). You
* may not use this file except in compliance with the License. You can
* obtain a copy of the License at
* https://github.com/payara/Payara/blob/master/LICENSE.txt
* See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each
* file and include the License file at glassfish/legal/LICENSE.txt.
*
* GPL Classpath Exception:
* The Payara Foundation designates this particular file as subject to the "Classpath"
* exception as provided by the Payara Foundation in the GPL Version 2 section of the License
* file that accompanied this code.
*
* Modifications:
* If applicable, add the following below the License Header, with the fields
* enclosed by brackets [] replaced by your own identifying information:
* "Portions Copyright [year] [name of copyright owner]"
*
* Contributor(s):
* If you wish your version of this file to be governed by only the CDDL or
* only the GPL Version 2, indicate your decision by adding "[Contributor]
* elects to include this software in this distribution under the [CDDL or GPL
* Version 2] license." If you don't indicate a single choice of license, a
* recipient has the option to distribute your version of this file under
* either the CDDL, the GPL Version 2 or to extend the choice of license to
* its licensees as provided above. However, if you add GPL Version 2 code
* and therefore, elected the GPL Version 2 license, then the option applies
* only if the new code is made subject to such option by the copyright
* holder.
*/
package fish.payara.gradle.plugins.micro;

public class JakartaEEVersionSelector {

public static int getMajorJakartaEEVersion() {
int jdkVersion = getCurrentJDKVersion();
switch (jdkVersion) {
case 21:
return 11; // Select 11 for JDK 21
case 17:
return 10; // Select 10 for JDK 17
case 11:
return 8; // Select 8 for JDK 11
default:
return 8;
}
}

private static int getCurrentJDKVersion() {
String version = System.getProperty("java.version");
String[] versionParts = version.split("\\.");
int majorVersion;

if (versionParts[0].equals("1")) {
majorVersion = Integer.parseInt(versionParts[1]); // For versions like 1.8
} else {
majorVersion = Integer.parseInt(versionParts[0]); // For versions like 9, 10, 11
}

return majorVersion;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,12 @@ public class StartStopLifecycleTest extends BaseTest {
public void warTest() throws Exception {

buildProject();
buildExtension().setDeployWar(true);

PayaraMicroExtension extension = buildExtension();
int jakataEEVersion = JakartaEEVersionSelector.getMajorJakartaEEVersion();
String payaraVersion = PayaraMicroVersionSelector.getPayaraMicroVersion(jakataEEVersion);
extension.setPayaraVersion(payaraVersion);
extension.setDeployWar(true);

createWar();
bootstrapMicro();
Expand All @@ -66,17 +71,23 @@ public void customMicroVersionTest() throws Exception {
public void uberJarTest() throws Exception {

buildProject();
buildExtension().setUseUberJar(true);
PayaraMicroExtension extension = buildExtension();
int jakataEEVersion = JakartaEEVersionSelector.getMajorJakartaEEVersion();
String payaraVersion = PayaraMicroVersionSelector.getPayaraMicroVersion(jakataEEVersion);
extension.setPayaraVersion(payaraVersion);
extension.setUseUberJar(true);

bundleMicro();
bootstrapMicro();
}

@Test
public void explodedWarTest() throws Exception {

buildProject();
PayaraMicroExtension extension = buildExtension();
int jakataEEVersion = JakartaEEVersionSelector.getMajorJakartaEEVersion();
String payaraVersion = PayaraMicroVersionSelector.getPayaraMicroVersion(jakataEEVersion);
extension.setPayaraVersion(payaraVersion);
extension.setDeployWar(true);
extension.setExploded(true);

Expand Down

0 comments on commit c2bc023

Please sign in to comment.