Skip to content

Commit

Permalink
feat(assemble) Configurable environment variables in launcher scripts
Browse files Browse the repository at this point in the history
Closes #1671
  • Loading branch information
aalmiray committed Jun 7, 2024
1 parent b23d6f1 commit fb2a06f
Show file tree
Hide file tree
Showing 22 changed files with 590 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,11 @@ public interface Constants {
String KEY_DISTRIBUTION_JAVA_MAIN_CLASS = "distributionJavaMainClass";
String KEY_DISTRIBUTION_JAVA_MAIN_MODULE = "distributionJavaMainModule";
String KEY_DISTRIBUTION_JAVA_OPTIONS = "distributionJavaOptions";
String KEY_DISTRIBUTION_JAVA_ENVIRONMENT_VARIABLES_UNIVERSAL = "distributionJavaEnvironmentVariablesUniversal";
String KEY_DISTRIBUTION_JAVA_ENVIRONMENT_VARIABLES_UNIX = "distributionJavaEnvironmentVariablesUnix";
String KEY_DISTRIBUTION_JAVA_ENVIRONMENT_VARIABLES_LINUX = "distributionJavaEnvironmentVariablesLinux";
String KEY_DISTRIBUTION_JAVA_ENVIRONMENT_VARIABLES_OSX = "distributionJavaEnvironmentVariablesOsx";
String KEY_DISTRIBUTION_JAVA_ENVIRONMENT_VARIABLES_WINDOWS = "distributionJavaEnvironmentVariablesWindows";

// Artifact
String KEY_ARTIFACT_PLATFORM = "artifactPlatform";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.jreleaser.model.api.common.ArchiveOptions;
import org.jreleaser.model.api.common.Artifact;
import org.jreleaser.model.api.common.Domain;
import org.jreleaser.model.api.common.EnvironmentVariables;
import org.jreleaser.model.api.common.Executable;
import org.jreleaser.model.api.common.Glob;

Expand Down Expand Up @@ -54,5 +55,7 @@ interface Java extends Domain {
String getMainModule();

Set<String> getOptions();

EnvironmentVariables getEnvironmentVariables();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* Copyright 2020-2024 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.model.api.common;

import java.util.Map;

/**
* @author Andres Almiray
* @since 0.13.0
*/
public interface EnvironmentVariables extends Domain {
Map<String, String> getUniversal();

Map<String, String> getUnix();

Map<String, String> getLinux();

Map<String, String> getOsx();

Map<String, String> getWindows();
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,6 @@ public interface Java extends Domain, ExtraProperties, EnabledAware {
String getMainModule();

Set<String> getOptions();

EnvironmentVariables getEnvironmentVariables();
}
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ catalog.sbom.not.changed = All sboms are up-to-date. Skipping
catalog.sbom.pack = Packing {}
ERROR_catalog_unexpected_error_packing = Unexpected error when packing {}
catalog.slsa.not.changed = Attestations are up-to-date. Skipping
catalog.github.not.changed = Attestations are up-to-date. Skipping

uploaders.header = Uploading distributions and files
uploaders.not.enabled = Uploading is not enabled. Skipping
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
import static org.jreleaser.model.Constants.KEY_DISTRIBUTION_ASSEMBLE_DIRECTORY;
import static org.jreleaser.model.Constants.KEY_DISTRIBUTION_EXECUTABLE;
import static org.jreleaser.model.Constants.KEY_DISTRIBUTION_JAVA_ENVIRONMENT_VARIABLES_LINUX;
import static org.jreleaser.model.Constants.KEY_DISTRIBUTION_JAVA_ENVIRONMENT_VARIABLES_OSX;
import static org.jreleaser.model.Constants.KEY_DISTRIBUTION_JAVA_ENVIRONMENT_VARIABLES_UNIVERSAL;
import static org.jreleaser.model.Constants.KEY_DISTRIBUTION_JAVA_ENVIRONMENT_VARIABLES_UNIX;
import static org.jreleaser.model.Constants.KEY_DISTRIBUTION_JAVA_ENVIRONMENT_VARIABLES_WINDOWS;
import static org.jreleaser.model.Constants.KEY_DISTRIBUTION_JAVA_MAIN_CLASS;
import static org.jreleaser.model.Constants.KEY_DISTRIBUTION_JAVA_MAIN_JAR;
import static org.jreleaser.model.Constants.KEY_DISTRIBUTION_JAVA_MAIN_MODULE;
Expand Down Expand Up @@ -104,6 +109,16 @@ protected void fillAssemblerProperties(TemplateContext props) {
Set<String> javaOptions = assembler.getJava().getOptions();
props.set(KEY_DISTRIBUTION_JAVA_OPTIONS, !javaOptions.isEmpty() ? passThrough(join(" ", javaOptions)) : "");
props.set(KEY_DISTRIBUTION_EXECUTABLE, assembler.getExecutable().getName());
props.set(KEY_DISTRIBUTION_JAVA_ENVIRONMENT_VARIABLES_UNIVERSAL,
assembler.getJava().getEnvironmentVariables().getResolvedUniversal(context).entrySet());
props.set(KEY_DISTRIBUTION_JAVA_ENVIRONMENT_VARIABLES_UNIX,
assembler.getJava().getEnvironmentVariables().getResolvedUnix(context).entrySet());
props.set(KEY_DISTRIBUTION_JAVA_ENVIRONMENT_VARIABLES_LINUX,
assembler.getJava().getEnvironmentVariables().getResolvedLinux(context).entrySet());
props.set(KEY_DISTRIBUTION_JAVA_ENVIRONMENT_VARIABLES_OSX,
assembler.getJava().getEnvironmentVariables().getResolvedOsx(context).entrySet());
props.set(KEY_DISTRIBUTION_JAVA_ENVIRONMENT_VARIABLES_WINDOWS,
assembler.getJava().getEnvironmentVariables().getResolvedWindows(context).entrySet());
}

private void archive(Path workDirectory, Path assembleDirectory, String archiveName, Archive.Format format) throws AssemblerProcessingException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@
import static org.jreleaser.model.Constants.KEY_ARCHIVE_FORMAT;
import static org.jreleaser.model.Constants.KEY_DISTRIBUTION_ASSEMBLE_DIRECTORY;
import static org.jreleaser.model.Constants.KEY_DISTRIBUTION_EXECUTABLE;
import static org.jreleaser.model.Constants.KEY_DISTRIBUTION_JAVA_ENVIRONMENT_VARIABLES_LINUX;
import static org.jreleaser.model.Constants.KEY_DISTRIBUTION_JAVA_ENVIRONMENT_VARIABLES_OSX;
import static org.jreleaser.model.Constants.KEY_DISTRIBUTION_JAVA_ENVIRONMENT_VARIABLES_UNIVERSAL;
import static org.jreleaser.model.Constants.KEY_DISTRIBUTION_JAVA_ENVIRONMENT_VARIABLES_UNIX;
import static org.jreleaser.model.Constants.KEY_DISTRIBUTION_JAVA_ENVIRONMENT_VARIABLES_WINDOWS;
import static org.jreleaser.model.Constants.KEY_DISTRIBUTION_JAVA_MAIN_CLASS;
import static org.jreleaser.model.Constants.KEY_DISTRIBUTION_JAVA_MAIN_JAR;
import static org.jreleaser.model.Constants.KEY_DISTRIBUTION_JAVA_MAIN_MODULE;
Expand Down Expand Up @@ -85,6 +90,16 @@ protected void fillAssemblerProperties(TemplateContext props) {
Set<String> javaOptions = assembler.getJava().getOptions();
props.set(KEY_DISTRIBUTION_JAVA_OPTIONS, !javaOptions.isEmpty() ? passThrough(join(" ", javaOptions)) : "");
props.set(KEY_DISTRIBUTION_EXECUTABLE, assembler.getExecutable());
props.set(KEY_DISTRIBUTION_JAVA_ENVIRONMENT_VARIABLES_UNIVERSAL,
assembler.getJava().getEnvironmentVariables().getResolvedUniversal(context).entrySet());
props.set(KEY_DISTRIBUTION_JAVA_ENVIRONMENT_VARIABLES_UNIX,
assembler.getJava().getEnvironmentVariables().getResolvedUnix(context).entrySet());
props.set(KEY_DISTRIBUTION_JAVA_ENVIRONMENT_VARIABLES_LINUX,
assembler.getJava().getEnvironmentVariables().getResolvedLinux(context).entrySet());
props.set(KEY_DISTRIBUTION_JAVA_ENVIRONMENT_VARIABLES_OSX,
assembler.getJava().getEnvironmentVariables().getResolvedOsx(context).entrySet());
props.set(KEY_DISTRIBUTION_JAVA_ENVIRONMENT_VARIABLES_WINDOWS,
assembler.getJava().getEnvironmentVariables().getResolvedWindows(context).entrySet());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.jreleaser.model.internal.common.ArchiveOptions;
import org.jreleaser.model.internal.common.Artifact;
import org.jreleaser.model.internal.common.Domain;
import org.jreleaser.model.internal.common.EnvironmentVariables;
import org.jreleaser.model.internal.common.Executable;
import org.jreleaser.model.internal.common.FileSet;
import org.jreleaser.model.internal.common.Glob;
Expand Down Expand Up @@ -349,15 +350,16 @@ protected void asMap(boolean full, Map<String, Object> props) {
}

public static final class Java extends AbstractModelObject<org.jreleaser.model.internal.assemble.JavaArchiveAssembler.Java> implements Domain {
private static final long serialVersionUID = -5383920072074242097L;
private static final long serialVersionUID = 1929919029370959487L;

private final EnvironmentVariables environmentVariables = new EnvironmentVariables();
private final Set<String> options = new LinkedHashSet<>();
private String mainModule;
private String mainClass;

@JsonIgnore
private final org.jreleaser.model.api.assemble.JavaArchiveAssembler.Java immutable = new org.jreleaser.model.api.assemble.JavaArchiveAssembler.Java() {
private static final long serialVersionUID = -2130856687512099219L;
private static final long serialVersionUID = 41087751148311519L;

@Override
public String getMainClass() {
Expand All @@ -374,6 +376,11 @@ public Set<String> getOptions() {
return unmodifiableSet(options);
}

@Override
public org.jreleaser.model.api.common.EnvironmentVariables getEnvironmentVariables() {
return environmentVariables.asImmutable();
}

@Override
public Map<String, Object> asMap(boolean full) {
return unmodifiableMap(org.jreleaser.model.internal.assemble.JavaArchiveAssembler.Java.this.asMap(full));
Expand All @@ -389,6 +396,7 @@ public void merge(org.jreleaser.model.internal.assemble.JavaArchiveAssembler.Jav
this.mainModule = merge(this.mainModule, source.mainModule);
this.mainClass = merge(this.mainClass, source.mainClass);
setOptions(merge(this.options, source.options));
setEnvironmentVariables(source.environmentVariables);
}

public String getMainClass() {
Expand Down Expand Up @@ -420,17 +428,27 @@ public void addOptions(Set<String> options) {
this.options.addAll(options);
}

public EnvironmentVariables getEnvironmentVariables() {
return environmentVariables;
}

public void setEnvironmentVariables(EnvironmentVariables environmentVariables) {
this.environmentVariables.merge(environmentVariables);
}

public boolean isSet() {
return isNotBlank(mainModule) ||
isNotBlank(mainClass) ||
!options.isEmpty();
!options.isEmpty() ||
environmentVariables.isSet();
}

@Override
public Map<String, Object> asMap(boolean full) {
Map<String, Object> map = new LinkedHashMap<>();
if (isNotBlank(mainModule)) map.put("mainModule", mainModule);
if (isNotBlank(mainClass)) map.put("mainClass", mainClass);
map.put("environmentVariables", environmentVariables);
if (!options.isEmpty()) map.put("options", options);
return map;
}
Expand Down
Loading

0 comments on commit fb2a06f

Please sign in to comment.