• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

jreleaser / jreleaser / #512

27 Jul 2025 05:51PM UTC coverage: 45.153% (-0.7%) from 45.803%
#512

push

github

aalmiray
feat(core): Add a --deploy flag to config

Closes #1946

3 of 11 new or added lines in 4 files covered. (27.27%)

845 existing lines in 34 files now uncovered.

23625 of 52322 relevant lines covered (45.15%)

0.45 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

68.81
/plugins/jreleaser/src/main/java/org/jreleaser/cli/AbstractModelCommand.java
1
/*
2
 * SPDX-License-Identifier: Apache-2.0
3
 *
4
 * Copyright 2020-2025 The JReleaser authors.
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *     https://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
package org.jreleaser.cli;
19

20
import org.jreleaser.config.JReleaserConfigParser;
21
import org.jreleaser.engine.context.ContextCreator;
22
import org.jreleaser.model.JReleaserVersion;
23
import org.jreleaser.model.api.JReleaserCommand;
24
import org.jreleaser.model.api.JReleaserContext.Mode;
25
import org.jreleaser.model.internal.JReleaserContext;
26
import org.jreleaser.model.internal.JReleaserModel;
27
import org.jreleaser.model.internal.environment.Environment;
28
import org.jreleaser.util.Env;
29
import org.jreleaser.util.PlatformUtils;
30
import org.jreleaser.util.StringUtils;
31
import picocli.CommandLine;
32

33
import java.nio.file.Files;
34
import java.nio.file.Path;
35
import java.nio.file.Paths;
36
import java.util.Arrays;
37
import java.util.Collections;
38
import java.util.LinkedHashSet;
39
import java.util.List;
40
import java.util.Optional;
41
import java.util.Properties;
42
import java.util.ServiceLoader;
43
import java.util.Set;
44

45
import static java.util.stream.Collectors.toList;
46
import static org.jreleaser.util.FileUtils.resolveOutputDirectory;
47
import static org.jreleaser.util.StringUtils.isBlank;
48
import static org.jreleaser.util.StringUtils.isNotBlank;
49

50
/**
51
 * @author Andres Almiray
52
 * @since 0.1.0
53
 */
54
public abstract class AbstractModelCommand<C extends IO> extends AbstractLoggingCommand<C> {
1✔
55
    @CommandLine.Option(names = {"-c", "--config-file"}, paramLabel = "<file>")
56
    Path configFile;
57

58
    @CommandLine.Option(names = {"--settings-file"}, paramLabel = "<file>")
59
    Path settingsFile;
60

61
    @CommandLine.Option(names = {"-grs", "--git-root-search"})
62
    Boolean gitRootSearch;
63

64
    @CommandLine.Option(names = {"--yolo"})
65
    Boolean yolo;
66

67
    @CommandLine.Option(names = {"--strict"})
68
    Boolean strict;
69

70
    @CommandLine.Option(names = {"-P", "--set-property"},
71
        paramLabel = "<key=value>")
72
    String[] properties;
73

74
    Path actualConfigFile;
75
    Path actualBasedir;
76

77
    @Override
78
    protected void collectCandidateDeprecatedArgs(Set<AbstractCommand.DeprecatedArg> args) {
79
        super.collectCandidateDeprecatedArgs(args);
1✔
80
        args.add(new DeprecatedArg("-grs", "--git-root-search", "1.5.0"));
1✔
81
    }
1✔
82

83
    @Override
84
    protected void execute() {
85
        resolveConfigFile();
1✔
86
        resolveBasedir();
1✔
87
        initLogger();
1✔
88
        PlatformUtils.resolveCurrentPlatform(logger);
1✔
89
        logger.info("JReleaser {}", JReleaserVersion.getPlainVersion());
1✔
90
        JReleaserVersion.banner(logger.getTracer());
1✔
91
        logger.info($("TEXT_config_file"), actualConfigFile);
1✔
92
        logger.increaseIndent();
1✔
93
        logger.info($("TEXT_basedir_set"), actualBasedir.toAbsolutePath());
1✔
94
        logger.info($("TEXT_outputdir_set"), getOutputDirectory().toAbsolutePath());
1✔
95
        logger.decreaseIndent();
1✔
96
        doExecute(createContext());
1✔
97
    }
1✔
98

99
    private void resolveConfigFile() {
100
        if (null != configFile) {
1✔
UNCOV
101
            actualConfigFile = configFile.normalize();
×
102
        } else {
103
            Path directory = Paths.get(".").normalize();
1✔
104
            Optional<Path> file = resolveConfigFileAt(directory);
1✔
105
            if (!file.isPresent() && null != basedir) {
1✔
UNCOV
106
                file = resolveConfigFileAt(basedir);
×
107
            }
108
            actualConfigFile = file.orElse(null);
1✔
109
        }
110

111
        if (null == actualConfigFile || !Files.exists(actualConfigFile)) {
1✔
112
            spec.commandLine().getErr()
×
113
                .println(spec.commandLine()
×
UNCOV
114
                    .getColorScheme()
×
115
                    .errorText($("ERROR_missing_config_file",
×
116
                        String.join("|", getSupportedConfigFormats())
×
117
                    )));
UNCOV
118
            spec.commandLine().usage(parent().getOut());
×
UNCOV
119
            throw new HaltExecutionException();
×
120
        }
121
    }
1✔
122

123
    private Optional<Path> resolveConfigFileAt(Path directory) {
124
        ServiceLoader<JReleaserConfigParser> parsers = ServiceLoader.load(JReleaserConfigParser.class,
1✔
125
            JReleaserConfigParser.class.getClassLoader());
1✔
126

127
        for (JReleaserConfigParser parser : parsers) {
1✔
128
            Path file = directory.resolve("jreleaser." + parser.getPreferredFileExtension()).normalize();
1✔
129
            if (Files.exists(file)) {
1✔
130
                return Optional.of(file);
1✔
131
            }
132
        }
1✔
133

UNCOV
134
        return Optional.empty();
×
135
    }
136

137
    private void resolveBasedir() {
138
        String resolvedBasedir = Env.resolve(org.jreleaser.model.api.JReleaserContext.BASEDIR, null != basedir ? basedir.toString() : "");
1✔
139
        actualBasedir = (isNotBlank(resolvedBasedir) ? Paths.get(resolvedBasedir) : actualConfigFile.toAbsolutePath().getParent()).normalize();
1✔
140
        if (!Files.exists(actualBasedir)) {
1✔
141
            spec.commandLine().getErr()
×
142
                .println(spec.commandLine().getColorScheme().errorText(
×
UNCOV
143
                    $("ERROR_missing_required_option", "--basedir=<basedir>")));
×
UNCOV
144
            spec.commandLine().usage(parent().getOut());
×
UNCOV
145
            throw new HaltExecutionException();
×
146
        }
147
    }
1✔
148

149
    private Path resolveSettings() {
UNCOV
150
        if (null != settingsFile) {
×
UNCOV
151
            return actualBasedir.resolve(settingsFile).normalize();
×
152
        }
153

UNCOV
154
        return null;
×
155
    }
156

157
    protected abstract void doExecute(JReleaserContext context);
158

159
    protected JReleaserContext createContext() {
160
        JReleaserModel model = ContextCreator.resolveModel(logger, actualConfigFile);
1✔
161
        Environment.PropertiesSource propertiesSource = new Environment.PropertiesPropertiesSource(collectProperties());
1✔
162
        model.getEnvironment().setPropertiesSource(propertiesSource);
1✔
163

164
        return ContextCreator.create(
1✔
165
            logger,
166
            resolveConfigurer(actualConfigFile),
1✔
167
            getMode(),
1✔
168
            getCommand(),
1✔
169
            model,
170
            actualBasedir,
171
            settingsFile,
172
            getOutputDirectory(),
1✔
173
            resolveBoolean(org.jreleaser.model.api.JReleaserContext.YOLO, yolo()),
1✔
174
            resolveBoolean(org.jreleaser.model.api.JReleaserContext.DRY_RUN, dryrun()),
1✔
175
            resolveBoolean(org.jreleaser.model.api.JReleaserContext.GIT_ROOT_SEARCH, gitRootSearch()),
1✔
176
            resolveBoolean(org.jreleaser.model.api.JReleaserContext.STRICT, strict()),
1✔
177
            collectSelectedPlatforms(),
1✔
178
            collectRejectedPlatforms());
1✔
179
    }
180

181
    protected boolean resolveBoolean(String key, Boolean value) {
182
        if (null != value) return value;
1✔
183
        String resolvedValue = Env.resolve(key, "");
1✔
184
        return isNotBlank(resolvedValue) && Boolean.parseBoolean(resolvedValue);
1✔
185
    }
186

187
    protected List<String> resolveCollection(String key, List<String> values) {
188
        if (!values.isEmpty()) return values;
1✔
189
        String resolvedValue = Env.resolve(key, "");
1✔
190
        if (isBlank(resolvedValue)) return Collections.emptyList();
1✔
191
        return Arrays.stream(resolvedValue.trim().split(","))
×
UNCOV
192
            .map(String::trim)
×
193
            .filter(StringUtils::isNotBlank)
×
UNCOV
194
            .collect(toList());
×
195
    }
196

197
    protected JReleaserContext.Configurer resolveConfigurer(Path configFile) {
198
        switch (StringUtils.getFilenameExtension(configFile.getFileName().toString())) {
1✔
199
            case "yml":
200
            case "yaml":
201
                return JReleaserContext.Configurer.CLI_YAML;
1✔
202
            case "toml":
UNCOV
203
                return JReleaserContext.Configurer.CLI_TOML;
×
204
            case "json":
UNCOV
205
                return JReleaserContext.Configurer.CLI_JSON;
×
206
            default:
207
                // should not happen!
UNCOV
208
                throw new IllegalArgumentException($("ERROR_invalid_config_format", configFile.getFileName()));
×
209
        }
210
    }
211

212
    @Override
213
    protected Path getOutputDirectory() {
214
        return resolveOutputDirectory(actualBasedir, outputdir, "out");
1✔
215
    }
216

217
    protected Boolean dryrun() {
218
        return false;
1✔
219
    }
220

221
    protected Boolean yolo() {
222
        return yolo;
1✔
223
    }
224

225
    protected Boolean strict() {
226
        return strict;
1✔
227
    }
228

229
    protected Boolean gitRootSearch() {
230
        return gitRootSearch;
1✔
231
    }
232

233
    private Set<String> getSupportedConfigFormats() {
UNCOV
234
        Set<String> extensions = new LinkedHashSet<>();
×
235

UNCOV
236
        ServiceLoader<JReleaserConfigParser> parsers = ServiceLoader.load(JReleaserConfigParser.class,
×
UNCOV
237
            JReleaserConfigParser.class.getClassLoader());
×
238

UNCOV
239
        for (JReleaserConfigParser parser : parsers) {
×
UNCOV
240
            extensions.add("." + parser.getPreferredFileExtension());
×
UNCOV
241
        }
×
242

UNCOV
243
        return extensions;
×
244
    }
245

246
    protected Mode getMode() {
247
        return Mode.FULL;
1✔
248
    }
249

250
    protected abstract JReleaserCommand getCommand();
251

252
    protected List<String> collectSelectedPlatforms() {
253
        return Collections.emptyList();
1✔
254
    }
255

256
    protected List<String> collectRejectedPlatforms() {
257
        return Collections.emptyList();
1✔
258
    }
259

260
    protected Properties collectProperties() {
261
        Properties props = new Properties();
1✔
262

263
        if (null != properties && properties.length > 0) {
1✔
264
            for (String property : properties) {
1✔
265
                if (property.contains("=")) {
1✔
266
                    int d = property.indexOf('=');
1✔
267
                    if (d == 0 || d == properties.length - 1) {
1✔
UNCOV
268
                        throw new IllegalArgumentException($("ERROR_invalid_property", property));
×
269
                    }
270
                    props.put(property.substring(0, d),
1✔
271
                        property.substring(d + 1));
1✔
272
                } else {
1✔
UNCOV
273
                    props.put(property, Boolean.TRUE);
×
274
                }
275
            }
276
        }
277

278
        return props;
1✔
279
    }
280
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc