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

jreleaser / jreleaser / #483

28 Apr 2025 02:14PM UTC coverage: 47.836% (-0.7%) from 48.55%
#483

push

github

aalmiray
feat(deploy): Increase delay/retries in Maven deployers

4 of 4 new or added lines in 2 files covered. (100.0%)

372 existing lines in 32 files now uncovered.

24819 of 51883 relevant lines covered (47.84%)

0.48 hits per line

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

67.31
/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 = {"-grs", "--git-root-search"})
59
    Boolean gitRootSearch;
60

61
    @CommandLine.Option(names = {"--strict"})
62
    Boolean strict;
63

64
    @CommandLine.Option(names = {"-P", "--set-property"},
65
        paramLabel = "<key=value>")
66
    String[] properties;
67

68
    Path actualConfigFile;
69
    Path actualBasedir;
70

71
    @Override
72
    protected void collectCandidateDeprecatedArgs(Set<AbstractCommand.DeprecatedArg> args) {
73
        super.collectCandidateDeprecatedArgs(args);
1✔
74
        args.add(new DeprecatedArg("-grs", "--git-root-search", "1.5.0"));
1✔
75
    }
1✔
76

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

93
    private void resolveConfigFile() {
94
        if (null != configFile) {
1✔
95
            actualConfigFile = configFile.normalize();
×
96
        } else {
97
            Path directory = Paths.get(".").normalize();
1✔
98
            Optional<Path> file = resolveConfigFileAt(directory);
1✔
99
            if (!file.isPresent() && null != basedir) {
1✔
100
                file = resolveConfigFileAt(basedir);
×
101
            }
102
            actualConfigFile = file.orElse(null);
1✔
103
        }
104

105
        if (null == actualConfigFile || !Files.exists(actualConfigFile)) {
1✔
106
            spec.commandLine().getErr()
×
107
                .println(spec.commandLine()
×
108
                    .getColorScheme()
×
109
                    .errorText($("ERROR_missing_config_file",
×
110
                        String.join("|", getSupportedConfigFormats())
×
111
                    )));
112
            spec.commandLine().usage(parent().getOut());
×
113
            throw new HaltExecutionException();
×
114
        }
115
    }
1✔
116

117
    private Optional<Path> resolveConfigFileAt(Path directory) {
118
        ServiceLoader<JReleaserConfigParser> parsers = ServiceLoader.load(JReleaserConfigParser.class,
1✔
119
            JReleaserConfigParser.class.getClassLoader());
1✔
120

121
        for (JReleaserConfigParser parser : parsers) {
1✔
122
            Path file = directory.resolve("jreleaser." + parser.getPreferredFileExtension()).normalize();
1✔
123
            if (Files.exists(file)) {
1✔
124
                return Optional.of(file);
1✔
125
            }
126
        }
1✔
127

128
        return Optional.empty();
×
129
    }
130

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

143
    protected abstract void doExecute(JReleaserContext context);
144

145
    protected JReleaserContext createContext() {
146
        JReleaserModel model = ContextCreator.resolveModel(logger, actualConfigFile);
1✔
147
        Environment.PropertiesSource propertiesSource = new Environment.PropertiesPropertiesSource(collectProperties());
1✔
148
        model.getEnvironment().setPropertiesSource(propertiesSource);
1✔
149

150
        return ContextCreator.create(
1✔
151
            logger,
152
            resolveConfigurer(actualConfigFile),
1✔
153
            getMode(),
1✔
154
            getCommand(),
1✔
155
            model,
156
            actualBasedir,
157
            getOutputDirectory(),
1✔
158
            resolveBoolean(org.jreleaser.model.api.JReleaserContext.DRY_RUN, dryrun()),
1✔
159
            resolveBoolean(org.jreleaser.model.api.JReleaserContext.GIT_ROOT_SEARCH, gitRootSearch()),
1✔
160
            resolveBoolean(org.jreleaser.model.api.JReleaserContext.STRICT, strict()),
1✔
161
            collectSelectedPlatforms(),
1✔
162
            collectRejectedPlatforms());
1✔
163
    }
164

165
    protected boolean resolveBoolean(String key, Boolean value) {
166
        if (null != value) return value;
1✔
167
        String resolvedValue = Env.resolve(key, "");
1✔
168
        return isNotBlank(resolvedValue) && Boolean.parseBoolean(resolvedValue);
1✔
169
    }
170

171
    protected List<String> resolveCollection(String key, List<String> values) {
172
        if (!values.isEmpty()) return values;
1✔
173
        String resolvedValue = Env.resolve(key, "");
1✔
174
        if (isBlank(resolvedValue)) return Collections.emptyList();
1✔
175
        return Arrays.stream(resolvedValue.trim().split(","))
×
176
            .map(String::trim)
×
177
            .filter(StringUtils::isNotBlank)
×
178
            .collect(toList());
×
179
    }
180

181
    protected JReleaserContext.Configurer resolveConfigurer(Path configFile) {
182
        switch (StringUtils.getFilenameExtension(configFile.getFileName().toString())) {
1✔
183
            case "yml":
184
            case "yaml":
185
                return JReleaserContext.Configurer.CLI_YAML;
1✔
186
            case "toml":
187
                return JReleaserContext.Configurer.CLI_TOML;
×
188
            case "json":
189
                return JReleaserContext.Configurer.CLI_JSON;
×
190
            default:
191
                // should not happen!
192
                throw new IllegalArgumentException($("ERROR_invalid_config_format", configFile.getFileName()));
×
193
        }
194
    }
195

196
    @Override
197
    protected Path getOutputDirectory() {
198
        return resolveOutputDirectory(actualBasedir, outputdir, "out");
1✔
199
    }
200

201
    protected Boolean dryrun() {
202
        return false;
1✔
203
    }
204

205
    protected Boolean strict() {
206
        return strict;
1✔
207
    }
208

209
    protected Boolean gitRootSearch() {
210
        return gitRootSearch;
1✔
211
    }
212

213
    private Set<String> getSupportedConfigFormats() {
214
        Set<String> extensions = new LinkedHashSet<>();
×
215

216
        ServiceLoader<JReleaserConfigParser> parsers = ServiceLoader.load(JReleaserConfigParser.class,
×
217
            JReleaserConfigParser.class.getClassLoader());
×
218

219
        for (JReleaserConfigParser parser : parsers) {
×
220
            extensions.add("." + parser.getPreferredFileExtension());
×
221
        }
×
222

223
        return extensions;
×
224
    }
225

226
    protected Mode getMode() {
UNCOV
227
        return Mode.FULL;
×
228
    }
229

230
    protected abstract JReleaserCommand getCommand();
231

232
    protected List<String> collectSelectedPlatforms() {
UNCOV
233
        return Collections.emptyList();
×
234
    }
235

236
    protected List<String> collectRejectedPlatforms() {
UNCOV
237
        return Collections.emptyList();
×
238
    }
239

240
    protected Properties collectProperties() {
241
        Properties props = new Properties();
1✔
242

243
        if (null != properties && properties.length > 0) {
1✔
244
            for (String property : properties) {
1✔
245
                if (property.contains("=")) {
1✔
246
                    int d = property.indexOf('=');
1✔
247
                    if (d == 0 || d == properties.length - 1) {
1✔
248
                        throw new IllegalArgumentException($("ERROR_invalid_property", property));
×
249
                    }
250
                    props.put(property.substring(0, d),
1✔
251
                        property.substring(d + 1));
1✔
252
                } else {
1✔
253
                    props.put(property, Boolean.TRUE);
×
254
                }
255
            }
256
        }
257

258
        return props;
1✔
259
    }
260
}
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