Skip to content

Commit

Permalink
Add Java 22 test src
Browse files Browse the repository at this point in the history
  • Loading branch information
gjwatts committed Jan 31, 2024
1 parent 7c69aa8 commit 8d2f2f8
Show file tree
Hide file tree
Showing 11 changed files with 345 additions and 1 deletion.
6 changes: 6 additions & 0 deletions io.openliberty.java.internal_fat_22/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.gradle/
/bin/
/build/
/generated/
/gradle/
/wlp
89 changes: 89 additions & 0 deletions io.openliberty.java.internal_fat_22/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# Repo for quick testing of Liberty apps
Especially useful for new Java versions

### For versions of Java that already have gradle support

To build the WAR file locally, from the root directory of the open-liberty-misc repository run:

```
./gradlew io.openliberty.java.internal_fat_22:build
```

To build+run locally, this currently requires some manual effort:
* Note, the **build.gradle** file is configured to look for a Liberty install under `/wlp`. You might need to change it to point it to your local test Liberty install instead.
* Copy the `jvm.options` file and the contents of the `server.env` file from the project's `run` directory and add them to your `/wlp/usr/servers/io.openliberty.java.internal_fat_22Server/server.env` file (or equivalent location).
* Change to the root directory of this local `io.openliberty.java.internal_fat_22` repository and run:

```
./gradlew io.openliberty.java.internal_fat_22:start
```

---

### For versions of Java that do not have gradle support yet
Make the following updates in the following files (example given here as if you were working with pre-gradle support for Java 22):

- In **build.gradle**, set:

```
languageVersion = JavaLanguageVersion.of(22)
```

- And then set the environment variable JDK22 to your Java 22 JDK home, for example:

```
(Mac) export JDK22="/path/to/your/jdk22/home"
(Unix) JDK22="/path/to/your/jdk22/home"
(Win DOS) set JDK22="C:\path\to\your\jdk22\home"
(Win PS) $env:JDK22="C:\path\to\your\jdk22\home"
```

- To build the WAR file locally, from the root directory of the open-liberty-misc repository run:

```
./gradlew io.openliberty.java.internal_fat_22:build -P"org.gradle.java.installations.fromEnv=JDK22"
```
where **JDK22** is a system environment variable that reflects the location of your Java XX JDK you wish the source to be complied at (see last step).


To build+run locally, this currently requires some manual effort:
* Note, the **build.gradle** file is configured to look for a Liberty install under `/wlp`. You might need to change it to point it to your local test Liberty install instead.
* Copy the `jvm.options` file and the contents of the `server.env` file from the project's `run` directory and add them to your `/wlp/usr/servers/io.openliberty.java.internal_fat_22Server/server.env` file (or equivalent location).
* Change to the root directory of this local `io.openliberty.java.internal_fat_22` repository and run:

```
./gradlew io.openliberty.java.internal_fat_22:start -P"org.gradle.java.installations.fromEnv=JDK22"
```
where **JDK22** is a system environment variable that reflects the location of your Java XX JDK you wish the source to be complied at (see last step).


If testing with the `io.openliberty.java.internal_fat` FAT from OpenLiberty, copy the file gradle builds (in build/libs) `io.openliberty.java.internal_fat_22.war` to the `io.openliberty.java.internal_fat/publish/servers/java22-server/apps` directory.

---

### When moving to a new release of Java

Here are the locations where you might want to update up the Java release number to the current Java version

- **org.eclipse.wst.common.component** (in the `.settings` directory)

```
<wb-module deploy-name="io.openliberty.java.internal_fat_22">
<property name="context-root" value="io.openliberty.java.internal_fat_22"/>
```

- **build.gradle**

```
appUrl = 'http://localhost:9080/io.openliberty.java.internal_fat_22/'
<Make sure to add any new dependencies or update existing ones to the proper levels>
```

- **gradle-wrapper.properties** (might as well bump up to the latest supported version of gradle)

```
distributionUrl=https\://services.gradle.org/distributions/gradle-X.Y-bin.zip
```

Then add new code to **TestServices.java** either directly or via another class and make sure **TestApp.java** is in the same directory as it.
105 changes: 105 additions & 0 deletions io.openliberty.java.internal_fat_22/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*******************************************************************************
* Copyright (c) 2024 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
// To create this WAR file (ex: ./gradlew build...) for newer versions of Java (typically early access ones) before gradle supports it
// See the README.md file
// Change the installDir setting if your Liberty is located outside of /wlp

apply plugin: 'war'

description = "Basic Liberty repo"

tasks.withType(JavaCompile) {
options.encoding = 'UTF-8'
}

compileJava {
//inputs.property("moduleName", moduleName)
doFirst {
options.compilerArgs = [
'--module-path', classpath.asPath,
'--enable-preview'
]
classpath = files()
options.warnings = true
options.deprecation = true
options.debug = true
options.incremental = false
}
}
test {
useJUnitPlatform()
}

java {
toolchain {
languageVersion = JavaLanguageVersion.of(22)
}
}

buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'io.openliberty.tools:liberty-gradle-plugin:3.0'
}
}

repositories {
mavenCentral()
}

configurations {
serverLibs
}

dependencies {
compileOnly group: 'javax.ws.rs', name: 'javax.ws.rs-api', version: '2.1.1'
// compileOnly group: 'javax.persistence', name: 'javax.persistence-api', version: '2.2'
compileOnly group: 'javax.xml.bind', name: 'jaxb-api', version: '2.3.1'
compileOnly group: 'javax.enterprise', name: 'cdi-api', version: '2.0'
// compileOnly group: 'javax.annotation', name: 'javax.annotation-api', version: '1.3.2'
// compileOnly group: 'javax.transaction', name: 'javax.transaction-api', version: '1.3'
// libertyRuntime group: 'io.openliberty', name: 'openliberty-runtime', version: '22.0.0.12'
// serverLibs group: 'org.apache.derby', name: 'derby', version: '10.14.2.0' // '10.15.1.3'
}

ext {
appUrl = 'http://localhost:9080/io.openliberty.java.internal_fat_22/'
}

task copyServerLibs(type: Copy) {
from configurations.serverLibs
// Default location of local Liberty server
into "/wlp/usr/servers/${project.name}Server/lib"
}

// Not currently working, must manually handle
tasks.register('copyEnvFiles', Copy) {
println "Start of copyEnvFiles"
from file('run/jvm.options')
// Default location of local Liberty server
into new File("/wlp/usr/servers/${project.name}Server")
from file('run/server.env')
// Default location of local Liberty server
into new File("/wlp/usr/servers/${project.name}Server")
println "End of copyEnvFiles"
}

task openBrowser {
description = "Open browser to ${appUrl}"
doLast {
java.awt.Desktop.desktop.browse "${appUrl}".toURI()
}
}

5 changes: 5 additions & 0 deletions io.openliberty.java.internal_fat_22/run/jvm.options
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Enable Java preview features
--enable-preview

# Enable Java incubator modules
--add-modules=jdk.incubator.foreign
2 changes: 2 additions & 0 deletions io.openliberty.java.internal_fat_22/run/server.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Need to update the JAVA_HOME environment variable to point to your Java 22 JDK
JAVA_HOME=/jdk/hotspot/jdk22
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*******************************************************************************
* Copyright (c) 2024 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package io.openliberty.java.internal;

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

@ApplicationPath("/")
public class TestApp extends Application {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*******************************************************************************
* Copyright (c) 2024 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package io.openliberty.java.internal;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Arrays;

import javax.enterprise.context.ApplicationScoped;
import javax.ws.rs.GET;
import javax.ws.rs.Path;

@Path("/")
@ApplicationScoped
public class TestService {

private StringWriter sw = new StringWriter();

@GET
public String test() {
try {
log(">>> ENTER");
doTest();
log("<<< EXIT SUCCESSFUL");
} catch (Exception e) {
e.printStackTrace(System.out);
e.printStackTrace(new PrintWriter(sw));
log("<<< EXIT FAILED");
}
String result = sw.toString();
sw = new StringWriter();
return result;
}


private void doTest() throws Exception {
log("Beginning Java 22 testing");
ArrayList<String> favoriteShows = new ArrayList<String>(Arrays.asList("Outpost","Grimm","Community","Scrubs","Castle","Star Trek"));
int count = countElements(favoriteShows);

if (count != favoriteShows.size()) {
log("Failed testing");
throw new Exception("Number of elements counted in ArrayList (" + count + ") is not equal to the size (" + favoriteShows.size() + ")!");
}

log("Goodbye testing");
}

/**
* Demonstrate unnamed variables : JEP 456 -> https://openjdk.org/jeps/456
*
* @param shows
* @return
*/
public int countElements(ArrayList<String> list) {
total = 0;
for (var _ : list) { // Use _ for unnamed variable
total++;
}
return total;
}


public void log(String msg) {
System.out.println(msg);
sw.append(msg);
sw.append("<br/>");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*******************************************************************************
* Copyright (c) 2024 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package io.openliberty.java.internal;
17 changes: 17 additions & 0 deletions io.openliberty.java.internal_fat_22/src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*******************************************************************************
* Copyright (c) 2024 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
open module io.openliberty.java.internal.basic_app {

requires java.ws.rs;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<server>
<featureManager>
<feature>servlet-4.0</feature>
<feature>jaxrs-2.1</feature>
</featureManager>
</server>
3 changes: 2 additions & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2021, 2023 IBM Corporation and others.
* Copyright (c) 2021, 2024 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
Expand All @@ -17,3 +17,4 @@ include 'io.openliberty.java.internal_fat_18'
include 'io.openliberty.java.internal_fat_19'
include 'io.openliberty.java.internal_fat_20'
include 'io.openliberty.java.internal_fat_21'
include 'io.openliberty.java.internal_fat_22'

0 comments on commit 8d2f2f8

Please sign in to comment.