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

jreleaser / jreleaser / #550

01 Nov 2025 03:54PM UTC coverage: 47.949% (-0.3%) from 48.255%
#550

push

github

aalmiray
build: Update jdks to Java 25

25852 of 53916 relevant lines covered (47.95%)

0.48 hits per line

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

64.71
/plugins/jreleaser-ant-tasks/src/main/java/org/jreleaser/ant/tasks/AbstractJReleaserTask.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.ant.tasks;
19

20
import org.apache.tools.ant.BuildException;
21
import org.apache.tools.ant.Task;
22
import org.jreleaser.ant.tasks.internal.JReleaserLoggerAdapter;
23
import org.jreleaser.config.JReleaserConfigParser;
24
import org.jreleaser.engine.context.ContextCreator;
25
import org.jreleaser.logging.JReleaserLogger;
26
import org.jreleaser.model.JReleaserVersion;
27
import org.jreleaser.model.api.JReleaserCommand;
28
import org.jreleaser.model.api.JReleaserContext.Mode;
29
import org.jreleaser.model.internal.JReleaserContext;
30
import org.jreleaser.util.Env;
31
import org.jreleaser.util.PlatformUtils;
32
import org.jreleaser.util.StringUtils;
33

34
import java.io.File;
35
import java.io.FileOutputStream;
36
import java.io.IOException;
37
import java.io.PrintWriter;
38
import java.nio.file.Files;
39
import java.nio.file.Path;
40
import java.nio.file.Paths;
41
import java.util.ArrayList;
42
import java.util.Arrays;
43
import java.util.Collection;
44
import java.util.Collections;
45
import java.util.LinkedHashSet;
46
import java.util.List;
47
import java.util.Locale;
48
import java.util.ServiceLoader;
49
import java.util.Set;
50

51
import static java.util.stream.Collectors.toList;
52
import static org.jreleaser.util.FileUtils.resolveOutputDirectory;
53
import static org.jreleaser.util.IoUtils.newPrintWriter;
54
import static org.jreleaser.util.StringUtils.isBlank;
55
import static org.jreleaser.util.StringUtils.isNotBlank;
56

57
/**
58
 * @author Andres Almiray
59
 * @since 0.1.0
60
 */
61
abstract class AbstractJReleaserTask extends Task {
1✔
62
    protected File basedir;
63
    protected File settingsFile;
64
    protected File configFile;
65
    protected Boolean yolo;
66
    protected Boolean dryrun;
67
    protected Boolean gitRootSearch;
68
    protected Boolean strict;
69
    protected boolean skip;
70
    protected Path outputDir;
71

72
    protected JReleaserLogger logger;
73
    protected Path actualConfigFile;
74
    protected Path actualBasedir;
75

76
    public void setBasedir(File basedir) {
77
        this.basedir = basedir;
1✔
78
    }
1✔
79

80
    public void setSettingsFile(File settingsFile) {
81
        this.settingsFile = settingsFile;
1✔
82
    }
1✔
83

84
    public void setConfigFile(File configFile) {
85
        this.configFile = configFile;
1✔
86
    }
1✔
87

88
    public void setYolo(Boolean yolo) {
89
        this.yolo = yolo;
×
90
    }
×
91

92
    public void setDryrun(Boolean dryrun) {
93
        this.dryrun = dryrun;
1✔
94
    }
1✔
95

96
    public void setGitRootSearch(Boolean gitRootSearch) {
97
        this.gitRootSearch = gitRootSearch;
1✔
98
    }
1✔
99

100
    public void setStrict(Boolean strict) {
101
        this.strict = strict;
1✔
102
    }
1✔
103

104
    public void setSkip(boolean skip) {
105
        this.skip = skip;
1✔
106
    }
1✔
107

108
    public void setOutputDir(Path outputDir) {
109
        this.outputDir = outputDir;
1✔
110
    }
1✔
111

112
    @Override
113
    public void execute() throws BuildException {
114
        Banner.display(newPrintWriter(System.err));
1✔
115
        if (skip) return;
1✔
116

117
        resolveConfigFile();
1✔
118
        resolveBasedir();
1✔
119
        initLogger();
1✔
120
        PlatformUtils.resolveCurrentPlatform(logger);
1✔
121
        logger.info("JReleaser {}", JReleaserVersion.getPlainVersion());
1✔
122
        JReleaserVersion.banner(logger.getTracer());
1✔
123
        logger.info("Configuring with {}", actualConfigFile);
1✔
124
        logger.info(" - basedir set to {}", actualBasedir.toAbsolutePath());
1✔
125
        Path settings = resolveSettings();
1✔
126
        if (null != settings) {
1✔
127
            logger.info(" - settings set to {}", settings.toAbsolutePath());
1✔
128
        }
129
        logger.info(" - outputdir set to {}", getOutputDirectory().toAbsolutePath());
1✔
130
        doExecute(createContext());
1✔
131
    }
1✔
132

133
    private void resolveConfigFile() {
134
        if (null != configFile) {
1✔
135
            actualConfigFile = configFile.toPath();
1✔
136
        } else {
137
            ServiceLoader<JReleaserConfigParser> parsers = ServiceLoader.load(JReleaserConfigParser.class,
×
138
                JReleaserConfigParser.class.getClassLoader());
×
139

140
            for (JReleaserConfigParser parser : parsers) {
×
141
                Path file = Paths.get(".").normalize()
×
142
                    .resolve("jreleaser." + parser.getPreferredFileExtension());
×
143
                if (Files.exists(file)) {
×
144
                    actualConfigFile = file;
×
145
                    break;
×
146
                }
147
            }
×
148
        }
149

150
        if (null == actualConfigFile || !Files.exists(actualConfigFile)) {
1✔
151
            throw new BuildException("Missing required option 'configFile' " +
×
152
                "or local file named jreleaser[" +
153
                String.join("|", getSupportedConfigFormats()) + "]");
×
154
        }
155
    }
1✔
156

157
    private void resolveBasedir() {
158
        String resolvedBasedir = Env.resolve(org.jreleaser.model.api.JReleaserContext.BASEDIR, null != basedir ? basedir.getPath() : "");
1✔
159
        actualBasedir = (isNotBlank(resolvedBasedir) ? Paths.get(resolvedBasedir) : actualConfigFile.toAbsolutePath().getParent()).normalize();
1✔
160
    }
1✔
161

162
    private Path resolveSettings() {
163
        if (null != settingsFile) {
1✔
164
            return actualBasedir.resolve(settingsFile.toPath()).normalize();
1✔
165
        }
166

167
        return null;
×
168
    }
169

170
    protected abstract void doExecute(JReleaserContext context);
171

172
    protected JReleaserLogger initLogger() {
173
        if (null == logger) {
1✔
174
            logger = new JReleaserLoggerAdapter(createTracer(), getProject());
1✔
175
        }
176
        return logger;
1✔
177
    }
178

179
    protected PrintWriter createTracer() {
180
        try {
181
            Files.createDirectories(getOutputDirectory());
1✔
182
            return newPrintWriter(new FileOutputStream(
1✔
183
                getOutputDirectory().resolve("trace.log").toFile()));
1✔
184
        } catch (IOException e) {
×
185
            throw new IllegalStateException("Could not initialize trace file", e);
×
186
        }
187
    }
188

189
    protected Path getOutputDirectory() {
190
        return resolveOutputDirectory(actualBasedir, outputDir, "build");
1✔
191
    }
192

193
    protected JReleaserContext createContext() {
194
        return ContextCreator.create(
1✔
195
            logger,
196
            resolveConfigurer(actualConfigFile),
1✔
197
            getMode(),
1✔
198
            getCommand(),
1✔
199
            actualConfigFile,
200
            actualBasedir,
201
            resolveSettings(),
1✔
202
            getOutputDirectory(),
1✔
203
            resolveBoolean(org.jreleaser.model.api.JReleaserContext.YOLO, yolo),
1✔
204
            resolveBoolean(org.jreleaser.model.api.JReleaserContext.DRY_RUN, dryrun),
1✔
205
            resolveBoolean(org.jreleaser.model.api.JReleaserContext.GIT_ROOT_SEARCH, gitRootSearch),
1✔
206
            resolveBoolean(org.jreleaser.model.api.JReleaserContext.STRICT, strict),
1✔
207
            collectSelectedPlatforms(),
1✔
208
            collectRejectedPlatforms());
1✔
209
    }
210

211
    protected boolean resolveBoolean(String key, Boolean value) {
212
        if (null != value) return value;
1✔
213
        String resolvedValue = Env.resolve(key, "");
1✔
214
        return isNotBlank(resolvedValue) && Boolean.parseBoolean(resolvedValue);
1✔
215
    }
216

217
    protected List<String> resolveCollection(String key, List<String> values) {
218
        if (!values.isEmpty()) return values;
1✔
219
        String resolvedValue = Env.resolve(key, "");
1✔
220
        if (isBlank(resolvedValue)) return Collections.emptyList();
1✔
221
        return Arrays.stream(resolvedValue.trim().split(","))
×
222
            .map(String::trim)
×
223
            .filter(StringUtils::isNotBlank)
×
224
            .collect(toList());
×
225
    }
226

227
    protected JReleaserContext.Configurer resolveConfigurer(Path configFile) {
228
        switch (StringUtils.getFilenameExtension(configFile.getFileName().toString())) {
1✔
229
            case "yml":
230
            case "yaml":
231
                return JReleaserContext.Configurer.CLI_YAML;
1✔
232
            case "toml":
233
                return JReleaserContext.Configurer.CLI_TOML;
×
234
            case "json":
235
                return JReleaserContext.Configurer.CLI_JSON;
×
236
            default:
237
                // should not happen!
238
                throw new IllegalArgumentException("Invalid configuration format: " + configFile.getFileName());
×
239
        }
240
    }
241

242
    private Set<String> getSupportedConfigFormats() {
243
        Set<String> extensions = new LinkedHashSet<>();
×
244

245
        ServiceLoader<JReleaserConfigParser> parsers = ServiceLoader.load(JReleaserConfigParser.class,
×
246
            JReleaserConfigParser.class.getClassLoader());
×
247

248
        for (JReleaserConfigParser parser : parsers) {
×
249
            extensions.add("." + parser.getPreferredFileExtension());
×
250
        }
×
251

252
        return extensions;
×
253
    }
254

255
    protected Mode getMode() {
256
        return Mode.FULL;
1✔
257
    }
258

259
    protected abstract JReleaserCommand getCommand();
260

261
    protected List<String> collectSelectedPlatforms() {
262
        return Collections.emptyList();
1✔
263
    }
264

265
    protected List<String> collectRejectedPlatforms() {
266
        return Collections.emptyList();
1✔
267
    }
268

269
    protected List<String> collectEntries(List<String> input) {
270
        return collectEntries(input, false);
1✔
271
    }
272

273
    protected List<String> collectEntries(List<String> input, boolean lowerCase) {
274
        List<String> list = new ArrayList<>();
1✔
275
        if (null != input && !input.isEmpty()) {
1✔
276
            for (String s : input) {
×
277
                if (isNotBlank(s)) {
×
278
                    if (!s.contains("-") && lowerCase) {
×
279
                        s = StringUtils.getHyphenatedName(s);
×
280
                    }
281
                    list.add(lowerCase ? s.toLowerCase(Locale.ENGLISH) : s);
×
282
                }
283
            }
×
284
        }
285
        return list;
1✔
286
    }
287

288
    protected Collection<String> expandAndCollect(String input) {
289
        if (isBlank(input)) return Collections.emptyList();
1✔
290

291
        if (input.contains(",")) {
×
292
            return Arrays.stream(input.split(","))
×
293
                .map(String::trim)
×
294
                .filter(StringUtils::isNotBlank)
×
295
                .collect(toList());
×
296
        }
297

298
        return Collections.singletonList(input.trim());
×
299
    }
300
}
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