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

jreleaser / jreleaser / #502

30 Jun 2025 07:46PM UTC coverage: 49.362% (+0.002%) from 49.36%
#502

push

github

aalmiray
Bump for next development cycle

25732 of 52129 relevant lines covered (49.36%)

0.49 hits per line

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

62.73
/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 configFile;
64
    protected Boolean yolo;
65
    protected Boolean dryrun;
66
    protected Boolean gitRootSearch;
67
    protected Boolean strict;
68
    protected boolean skip;
69
    protected Path outputDir;
70

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

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

79
    public void setConfigFile(File configFile) {
80
        this.configFile = configFile;
1✔
81
    }
1✔
82

83
    public void setYolo(Boolean yolo) {
84
        this.yolo = yolo;
×
85
    }
×
86

87
    public void setDryrun(Boolean dryrun) {
88
        this.dryrun = dryrun;
1✔
89
    }
1✔
90

91
    public void setGitRootSearch(Boolean gitRootSearch) {
92
        this.gitRootSearch = gitRootSearch;
1✔
93
    }
1✔
94

95
    public void setStrict(Boolean strict) {
96
        this.strict = strict;
1✔
97
    }
1✔
98

99
    public void setSkip(boolean skip) {
100
        this.skip = skip;
1✔
101
    }
1✔
102

103
    public void setOutputDir(Path outputDir) {
104
        this.outputDir = outputDir;
1✔
105
    }
1✔
106

107
    @Override
108
    public void execute() throws BuildException {
109
        Banner.display(newPrintWriter(System.err));
1✔
110
        if (skip) return;
1✔
111

112
        resolveConfigFile();
1✔
113
        resolveBasedir();
1✔
114
        initLogger();
1✔
115
        PlatformUtils.resolveCurrentPlatform(logger);
1✔
116
        logger.info("JReleaser {}", JReleaserVersion.getPlainVersion());
1✔
117
        JReleaserVersion.banner(logger.getTracer());
1✔
118
        logger.info("Configuring with {}", actualConfigFile);
1✔
119
        logger.info(" - basedir set to {}", actualBasedir.toAbsolutePath());
1✔
120
        logger.info(" - outputdir set to {}", getOutputDirectory().toAbsolutePath());
1✔
121
        doExecute(createContext());
1✔
122
    }
1✔
123

124
    private void resolveConfigFile() {
125
        if (null != configFile) {
1✔
126
            actualConfigFile = configFile.toPath();
1✔
127
        } else {
128
            ServiceLoader<JReleaserConfigParser> parsers = ServiceLoader.load(JReleaserConfigParser.class,
×
129
                JReleaserConfigParser.class.getClassLoader());
×
130

131
            for (JReleaserConfigParser parser : parsers) {
×
132
                Path file = Paths.get(".").normalize()
×
133
                    .resolve("jreleaser." + parser.getPreferredFileExtension());
×
134
                if (Files.exists(file)) {
×
135
                    actualConfigFile = file;
×
136
                    break;
×
137
                }
138
            }
×
139
        }
140

141
        if (null == actualConfigFile || !Files.exists(actualConfigFile)) {
1✔
142
            throw new BuildException("Missing required option 'configFile' " +
×
143
                "or local file named jreleaser[" +
144
                String.join("|", getSupportedConfigFormats()) + "]");
×
145
        }
146
    }
1✔
147

148
    private void resolveBasedir() {
149
        String resolvedBasedir = Env.resolve(org.jreleaser.model.api.JReleaserContext.BASEDIR, null != basedir ? basedir.getPath() : "");
1✔
150
        actualBasedir = (isNotBlank(resolvedBasedir) ? Paths.get(resolvedBasedir) : actualConfigFile.toAbsolutePath().getParent()).normalize();
1✔
151
    }
1✔
152

153
    protected abstract void doExecute(JReleaserContext context);
154

155
    protected JReleaserLogger initLogger() {
156
        if (null == logger) {
1✔
157
            logger = new JReleaserLoggerAdapter(createTracer(), getProject());
1✔
158
        }
159
        return logger;
1✔
160
    }
161

162
    protected PrintWriter createTracer() {
163
        try {
164
            Files.createDirectories(getOutputDirectory());
1✔
165
            return newPrintWriter(new FileOutputStream(
1✔
166
                getOutputDirectory().resolve("trace.log").toFile()));
1✔
167
        } catch (IOException e) {
×
168
            throw new IllegalStateException("Could not initialize trace file", e);
×
169
        }
170
    }
171

172
    protected Path getOutputDirectory() {
173
        return resolveOutputDirectory(actualBasedir, outputDir, "build");
1✔
174
    }
175

176
    protected JReleaserContext createContext() {
177
        return ContextCreator.create(
1✔
178
            logger,
179
            resolveConfigurer(actualConfigFile),
1✔
180
            getMode(),
1✔
181
            getCommand(),
1✔
182
            actualConfigFile,
183
            actualBasedir,
184
            getOutputDirectory(),
1✔
185
            resolveBoolean(org.jreleaser.model.api.JReleaserContext.YOLO, yolo),
1✔
186
            resolveBoolean(org.jreleaser.model.api.JReleaserContext.DRY_RUN, dryrun),
1✔
187
            resolveBoolean(org.jreleaser.model.api.JReleaserContext.GIT_ROOT_SEARCH, gitRootSearch),
1✔
188
            resolveBoolean(org.jreleaser.model.api.JReleaserContext.STRICT, strict),
1✔
189
            collectSelectedPlatforms(),
1✔
190
            collectRejectedPlatforms());
1✔
191
    }
192

193
    protected boolean resolveBoolean(String key, Boolean value) {
194
        if (null != value) return value;
1✔
195
        String resolvedValue = Env.resolve(key, "");
1✔
196
        return isNotBlank(resolvedValue) && Boolean.parseBoolean(resolvedValue);
1✔
197
    }
198

199
    protected List<String> resolveCollection(String key, List<String> values) {
200
        if (!values.isEmpty()) return values;
1✔
201
        String resolvedValue = Env.resolve(key, "");
1✔
202
        if (isBlank(resolvedValue)) return Collections.emptyList();
1✔
203
        return Arrays.stream(resolvedValue.trim().split(","))
×
204
            .map(String::trim)
×
205
            .filter(StringUtils::isNotBlank)
×
206
            .collect(toList());
×
207
    }
208

209
    protected JReleaserContext.Configurer resolveConfigurer(Path configFile) {
210
        switch (StringUtils.getFilenameExtension(configFile.getFileName().toString())) {
1✔
211
            case "yml":
212
            case "yaml":
213
                return JReleaserContext.Configurer.CLI_YAML;
1✔
214
            case "toml":
215
                return JReleaserContext.Configurer.CLI_TOML;
×
216
            case "json":
217
                return JReleaserContext.Configurer.CLI_JSON;
×
218
            default:
219
                // should not happen!
220
                throw new IllegalArgumentException("Invalid configuration format: " + configFile.getFileName());
×
221
        }
222
    }
223

224
    private Set<String> getSupportedConfigFormats() {
225
        Set<String> extensions = new LinkedHashSet<>();
×
226

227
        ServiceLoader<JReleaserConfigParser> parsers = ServiceLoader.load(JReleaserConfigParser.class,
×
228
            JReleaserConfigParser.class.getClassLoader());
×
229

230
        for (JReleaserConfigParser parser : parsers) {
×
231
            extensions.add("." + parser.getPreferredFileExtension());
×
232
        }
×
233

234
        return extensions;
×
235
    }
236

237
    protected Mode getMode() {
238
        return Mode.FULL;
1✔
239
    }
240

241
    protected abstract JReleaserCommand getCommand();
242

243
    protected List<String> collectSelectedPlatforms() {
244
        return Collections.emptyList();
1✔
245
    }
246

247
    protected List<String> collectRejectedPlatforms() {
248
        return Collections.emptyList();
1✔
249
    }
250

251
    protected List<String> collectEntries(List<String> input) {
252
        return collectEntries(input, false);
1✔
253
    }
254

255
    protected List<String> collectEntries(List<String> input, boolean lowerCase) {
256
        List<String> list = new ArrayList<>();
1✔
257
        if (null != input && !input.isEmpty()) {
1✔
258
            for (String s : input) {
×
259
                if (isNotBlank(s)) {
×
260
                    if (!s.contains("-") && lowerCase) {
×
261
                        s = StringUtils.getHyphenatedName(s);
×
262
                    }
263
                    list.add(lowerCase ? s.toLowerCase(Locale.ENGLISH) : s);
×
264
                }
265
            }
×
266
        }
267
        return list;
1✔
268
    }
269

270
    protected Collection<String> expandAndCollect(String input) {
271
        if (isBlank(input)) return Collections.emptyList();
1✔
272

273
        if (input.contains(",")) {
×
274
            return Arrays.stream(input.split(","))
×
275
                .map(String::trim)
×
276
                .filter(StringUtils::isNotBlank)
×
277
                .collect(toList());
×
278
        }
279

280
        return Collections.singletonList(input.trim());
×
281
    }
282
}
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