Skip to content

Commit

Permalink
[assemble] support jpackage. Resolves #207
Browse files Browse the repository at this point in the history
  • Loading branch information
aalmiray committed Dec 16, 2021
1 parent 4362c41 commit 8b5e733
Show file tree
Hide file tree
Showing 65 changed files with 3,523 additions and 777 deletions.
Expand Up @@ -97,6 +97,16 @@ protected void writeFile(String content, Path outputFile) throws AssemblerProces
}
}

protected void writeFile(byte[] content, Path outputFile) throws AssemblerProcessingException {
try {
createDirectoriesWithFullAccess(outputFile.getParent());
Files.write(outputFile, content, CREATE, WRITE, TRUNCATE_EXISTING);
grantFullAccess(outputFile);
} catch (IOException e) {
throw new AssemblerProcessingException(RB.$("ERROR_unexpected_error_writing_file", outputFile.toAbsolutePath()), e);
}
}

protected Map<String, Object> fillProps(Map<String, Object> props) throws AssemblerProcessingException {
Map<String, Object> newProps = new LinkedHashMap<>(props);
context.getLogger().debug(RB.$("tool.fill.git.properties"));
Expand Down
Expand Up @@ -18,6 +18,7 @@
package org.jreleaser.assemblers;

import org.jreleaser.bundle.RB;
import org.jreleaser.model.Glob;
import org.jreleaser.model.JReleaserContext;
import org.jreleaser.model.JavaAssembler;
import org.jreleaser.model.Project;
Expand All @@ -28,8 +29,11 @@
import java.io.Reader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;

import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
import static org.jreleaser.templates.TemplateUtils.resolveAndMergeTemplates;
import static org.jreleaser.util.MustacheUtils.applyTemplate;

Expand Down Expand Up @@ -81,5 +85,29 @@ public void assemble(Map<String, Object> props) throws AssemblerProcessingExcept
}
}

protected Set<Path> copyFiles(JReleaserContext context, Path destination) throws AssemblerProcessingException {
Set<Path> paths = new LinkedHashSet<>();

// resolve all first
for (Glob glob : assembler.getFiles()) {
glob.getResolvedArtifacts(context).stream()
.map(artifact -> artifact.getResolvedPath(context, assembler))
.forEach(paths::add);
}

// copy all next
try {
Files.createDirectories(destination);
for (Path path : paths) {
context.getLogger().debug(RB.$("assembler.copying"), path.getFileName());
Files.copy(path, destination.resolve(path.getFileName()), REPLACE_EXISTING);
}
} catch (IOException e) {
throw new AssemblerProcessingException(RB.$("ERROR_assembler_copying_files"), e);
}

return paths;
}

protected abstract void writeFile(Project project, String content, Map<String, Object> props, String fileName) throws AssemblerProcessingException;
}
@@ -0,0 +1,102 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* Copyright 2020-2021 The JReleaser authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jreleaser.assemblers;

import org.jreleaser.bundle.RB;
import org.jreleaser.model.Glob;
import org.jreleaser.model.JReleaserContext;
import org.jreleaser.model.JavaAssembler;
import org.jreleaser.model.assembler.spi.AssemblerProcessingException;
import org.jreleaser.util.PlatformUtils;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.LinkedHashSet;
import java.util.Properties;
import java.util.Set;

import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
import static org.jreleaser.util.StringUtils.isBlank;
import static org.jreleaser.util.StringUtils.isNotBlank;

/**
* @author Andres Almiray
* @since 0.9.0
*/
public final class AssemblerUtils {
private static final String KEY_JAVA_VERSION = "JAVA_VERSION";

private AssemblerUtils() {
// noop
}

public static String readJavaVersion(Path path) throws AssemblerProcessingException {
Path release = path.resolve("release");
if (!Files.exists(release)) {
throw new AssemblerProcessingException(RB.$("ERROR_assembler_invalid_jdk_release", path.toAbsolutePath()));
}

try {
Properties props = new Properties();
props.load(Files.newInputStream(release));
if (props.containsKey(KEY_JAVA_VERSION)) {
String version = props.getProperty(KEY_JAVA_VERSION);
if (version.startsWith("\"") && version.endsWith("\"")) {
return version.substring(1, version.length() - 1);
}
return version;
} else {
throw new AssemblerProcessingException(RB.$("ERROR_assembler_invalid_jdk_release_file", release.toAbsolutePath()));
}
} catch (IOException e) {
throw new AssemblerProcessingException(RB.$("ERROR_assembler_invalid_jdk_release_file", release.toAbsolutePath()), e);
}
}

public static Set<Path> copyJars(JReleaserContext context, JavaAssembler assembler, Path jarsDirectory, String platform) throws AssemblerProcessingException {
Set<Path> paths = new LinkedHashSet<>();

// resolve all first
if (isBlank(platform)) {
paths.add(assembler.getMainJar().getEffectivePath(context, assembler));
}

for (Glob glob : assembler.getJars()) {
if ((isBlank(platform) && isBlank(glob.getPlatform())) ||
(isNotBlank(platform) && PlatformUtils.isCompatible(platform, glob.getPlatform()))) {
glob.getResolvedArtifacts(context).stream()
.map(artifact -> artifact.getResolvedPath(context, assembler))
.forEach(paths::add);
}
}

// copy all next
try {
Files.createDirectories(jarsDirectory);
for (Path path : paths) {
context.getLogger().debug(RB.$("assembler.copying"), path.getFileName());
Files.copy(path, jarsDirectory.resolve(path.getFileName()), REPLACE_EXISTING);
}
} catch (IOException e) {
throw new AssemblerProcessingException(RB.$("ERROR_assembler_copying_jars"), e);
}

return paths;
}
}
Expand Up @@ -20,7 +20,6 @@
import org.jreleaser.bundle.RB;
import org.jreleaser.model.Archive;
import org.jreleaser.model.Artifact;
import org.jreleaser.model.Glob;
import org.jreleaser.model.JReleaserContext;
import org.jreleaser.model.Jlink;
import org.jreleaser.model.Project;
Expand All @@ -37,13 +36,12 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
import static java.util.stream.Collectors.toSet;
import static org.jreleaser.assemblers.AssemblerUtils.copyJars;
import static org.jreleaser.assemblers.AssemblerUtils.readJavaVersion;
import static org.jreleaser.templates.TemplateUtils.trimTplExtension;
import static org.jreleaser.util.StringUtils.isBlank;
import static org.jreleaser.util.StringUtils.isNotBlank;
Expand All @@ -53,8 +51,6 @@
* @since 0.2.0
*/
public class JlinkAssemblerProcessor extends AbstractJavaAssemblerProcessor<Jlink> {
private static final String KEY_JAVA_VERSION = "JAVA_VERSION";

public JlinkAssemblerProcessor(JReleaserContext context) {
super(context);
}
Expand Down Expand Up @@ -93,10 +89,10 @@ protected void doAssemble(Map<String, Object> props) throws AssemblerProcessingE
Path jarsDirectory = inputsDirectory.resolve("jars");
Path universalJarsDirectory = jarsDirectory.resolve("universal");
context.getLogger().debug(RB.$("assembler.copy.jars"), context.relativizeToBasedir(universalJarsDirectory));
Set<Path> jars = copyJars(context, universalJarsDirectory, "");
Path platformJarsDirectory = inputsDirectory.resolve("jars").resolve(platform);
Set<Path> jars = copyJars(context, assembler, universalJarsDirectory, "");
Path platformJarsDirectory = jarsDirectory.resolve(platform);
context.getLogger().debug(RB.$("assembler.copy.jars"), context.relativizeToBasedir(platformJarsDirectory));
jars.addAll(copyJars(context, platformJarsDirectory, platform));
jars.addAll(copyJars(context, assembler, platformJarsDirectory, platform));

// resolve module names
Set<String> moduleNames = resolveModuleNames(context, jdkPath, jarsDirectory, platform);
Expand Down Expand Up @@ -143,7 +139,7 @@ private Artifact jlink(Path assembleDirectory, Path jdkPath, Artifact targetJdk,

// jlink it
String modulePath = targetJdk.getEffectivePath(context, assembler).resolve("jmods").toAbsolutePath().toString();
if (assembler.isCopyJars()) {
if (isNotBlank(assembler.getModuleName()) || assembler.isCopyJars()) {
modulePath += File.pathSeparator + jarsDirectory
.resolve("universal")
.toAbsolutePath();
Expand Down Expand Up @@ -244,84 +240,6 @@ private Artifact jlink(Path assembleDirectory, Path jdkPath, Artifact targetJdk,
}
}

private String readJavaVersion(Path path) throws AssemblerProcessingException {
Path release = path.resolve("release");
if (!Files.exists(release)) {
throw new AssemblerProcessingException(RB.$("ERROR_assembler_invalid_jdk_release", path.toAbsolutePath()));
}

try {
Properties props = new Properties();
props.load(Files.newInputStream(release));
if (props.containsKey(KEY_JAVA_VERSION)) {
String version = props.getProperty(KEY_JAVA_VERSION);
if (version.startsWith("\"") && version.endsWith("\"")) {
return version.substring(1, version.length() - 1);
}
return version;
} else {
throw new AssemblerProcessingException(RB.$("ERROR_assembler_invalid_jdk_release_file", release.toAbsolutePath()));
}
} catch (IOException e) {
throw new AssemblerProcessingException(RB.$("ERROR_assembler_invalid_jdk_release_file", release.toAbsolutePath()), e);
}
}

private Set<Path> copyJars(JReleaserContext context, Path jarsDirectory, String platform) throws AssemblerProcessingException {
Set<Path> paths = new LinkedHashSet<>();

// resolve all first
if (isBlank(platform)) {
paths.add(assembler.getMainJar().getEffectivePath(context, assembler));
}

for (Glob glob : assembler.getJars()) {
if ((isBlank(platform) && isBlank(glob.getPlatform())) ||
(isNotBlank(platform) && PlatformUtils.isCompatible(platform, glob.getPlatform()))) {
glob.getResolvedArtifacts(context).stream()
.map(artifact -> artifact.getResolvedPath(context, assembler))
.forEach(paths::add);
}
}

// copy all next
try {
Files.createDirectories(jarsDirectory);
for (Path path : paths) {
context.getLogger().debug(RB.$("assembler.copying"), path.getFileName());
Files.copy(path, jarsDirectory.resolve(path.getFileName()), REPLACE_EXISTING);
}
} catch (IOException e) {
throw new AssemblerProcessingException(RB.$("ERROR_assembler_copying_jars"), e);
}

return paths;
}

private Set<Path> copyFiles(JReleaserContext context, Path destination) throws AssemblerProcessingException {
Set<Path> paths = new LinkedHashSet<>();

// resolve all first
for (Glob glob : assembler.getFiles()) {
glob.getResolvedArtifacts(context).stream()
.map(artifact -> artifact.getResolvedPath(context, assembler))
.forEach(paths::add);
}

// copy all next
try {
Files.createDirectories(destination);
for (Path path : paths) {
context.getLogger().debug(RB.$("assembler.copying"), path.getFileName());
Files.copy(path, destination.resolve(path.getFileName()), REPLACE_EXISTING);
}
} catch (IOException e) {
throw new AssemblerProcessingException(RB.$("ERROR_assembler_copying_files"), e);
}

return paths;
}

private Set<String> resolveModuleNames(JReleaserContext context, Path jdkPath, Path jarsDirectory, String platform) throws AssemblerProcessingException {
if (!assembler.getModuleNames().isEmpty()) {
return assembler.getModuleNames();
Expand All @@ -337,7 +255,14 @@ private Set<String> resolveModuleNames(JReleaserContext context, Path jdkPath, P
cmd.arg("--ignore-missing-deps");
}
cmd.arg("--print-module-deps");
cmd.arg("--class-path");
if (isNotBlank(assembler.getModuleName())) {
cmd.arg("--module")
.arg(assembler.getModuleName())
.arg("--module-path");
} else {
cmd.arg("--class-path");
}

try {
Files.list(jarsDirectory.resolve("universal"))
.map(Path::toAbsolutePath)
Expand Down

0 comments on commit 8b5e733

Please sign in to comment.