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

jreleaser / jreleaser / #477

04 Apr 2025 05:53PM UTC coverage: 35.124% (-5.1%) from 40.183%
#477

push

github

aalmiray
fix(deploy): Add missing Forgejo messages

Related to #1842

18210 of 51845 relevant lines covered (35.12%)

0.35 hits per line

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

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

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

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

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

82
    public void setDryrun(Boolean dryrun) {
83
        this.dryrun = dryrun;
×
84
    }
×
85

86
    public void setGitRootSearch(Boolean gitRootSearch) {
87
        this.gitRootSearch = gitRootSearch;
1✔
88
    }
1✔
89

90
    public void setStrict(Boolean strict) {
91
        this.strict = strict;
1✔
92
    }
1✔
93

94
    public void setSkip(boolean skip) {
95
        this.skip = skip;
1✔
96
    }
1✔
97

98
    public void setOutputDir(Path outputDir) {
99
        this.outputDir = outputDir;
1✔
100
    }
1✔
101

102
    @Override
103
    public void execute() throws BuildException {
104
        Banner.display(newPrintWriter(System.err));
1✔
105
        if (skip) return;
1✔
106

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

119
    private void resolveConfigFile() {
120
        if (null != configFile) {
1✔
121
            actualConfigFile = configFile.toPath();
1✔
122
        } else {
123
            ServiceLoader<JReleaserConfigParser> parsers = ServiceLoader.load(JReleaserConfigParser.class,
×
124
                JReleaserConfigParser.class.getClassLoader());
×
125

126
            for (JReleaserConfigParser parser : parsers) {
×
127
                Path file = Paths.get(".").normalize()
×
128
                    .resolve("jreleaser." + parser.getPreferredFileExtension());
×
129
                if (Files.exists(file)) {
×
130
                    actualConfigFile = file;
×
131
                    break;
×
132
                }
133
            }
×
134
        }
135

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

143
    private void resolveBasedir() {
144
        String resolvedBasedir = Env.resolve(org.jreleaser.model.api.JReleaserContext.BASEDIR, null != basedir ? basedir.getPath() : "");
1✔
145
        actualBasedir = (isNotBlank(resolvedBasedir) ? Paths.get(resolvedBasedir) : actualConfigFile.toAbsolutePath().getParent()).normalize();
1✔
146
    }
1✔
147

148
    protected abstract void doExecute(JReleaserContext context);
149

150
    protected JReleaserLogger initLogger() {
151
        if (null == logger) {
1✔
152
            logger = new JReleaserLoggerAdapter(createTracer(), getProject());
1✔
153
        }
154
        return logger;
1✔
155
    }
156

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

167
    protected Path getOutputDirectory() {
168
        return resolveOutputDirectory(actualBasedir, outputDir, "build");
1✔
169
    }
170

171
    protected JReleaserContext createContext() {
172
        return ContextCreator.create(
1✔
173
            logger,
174
            resolveConfigurer(actualConfigFile),
1✔
175
            getMode(),
1✔
176
            getCommand(),
1✔
177
            actualConfigFile,
178
            actualBasedir,
179
            getOutputDirectory(),
1✔
180
            resolveBoolean(org.jreleaser.model.api.JReleaserContext.DRY_RUN, dryrun),
1✔
181
            resolveBoolean(org.jreleaser.model.api.JReleaserContext.GIT_ROOT_SEARCH, gitRootSearch),
1✔
182
            resolveBoolean(org.jreleaser.model.api.JReleaserContext.STRICT, strict),
1✔
183
            collectSelectedPlatforms(),
1✔
184
            collectRejectedPlatforms());
1✔
185
    }
186

187
    protected boolean resolveBoolean(String key, Boolean value) {
188
        if (null != value) return value;
1✔
189
        String resolvedValue = Env.resolve(key, "");
1✔
190
        return isNotBlank(resolvedValue) && Boolean.parseBoolean(resolvedValue);
1✔
191
    }
192

193
    protected List<String> resolveCollection(String key, List<String> values) {
194
        if (!values.isEmpty()) return values;
1✔
195
        String resolvedValue = Env.resolve(key, "");
1✔
196
        if (isBlank(resolvedValue)) return Collections.emptyList();
1✔
197
        return Arrays.stream(resolvedValue.trim().split(","))
×
198
            .map(String::trim)
×
199
            .filter(StringUtils::isNotBlank)
×
200
            .collect(toList());
×
201
    }
202

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

218
    private Set<String> getSupportedConfigFormats() {
219
        Set<String> extensions = new LinkedHashSet<>();
×
220

221
        ServiceLoader<JReleaserConfigParser> parsers = ServiceLoader.load(JReleaserConfigParser.class,
×
222
            JReleaserConfigParser.class.getClassLoader());
×
223

224
        for (JReleaserConfigParser parser : parsers) {
×
225
            extensions.add("." + parser.getPreferredFileExtension());
×
226
        }
×
227

228
        return extensions;
×
229
    }
230

231
    protected Mode getMode() {
232
        return Mode.FULL;
×
233
    }
234

235
    protected abstract JReleaserCommand getCommand();
236

237
    protected List<String> collectSelectedPlatforms() {
238
        return Collections.emptyList();
×
239
    }
240

241
    protected List<String> collectRejectedPlatforms() {
242
        return Collections.emptyList();
×
243
    }
244

245
    protected List<String> collectEntries(List<String> input) {
246
        return collectEntries(input, false);
1✔
247
    }
248

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

264
    protected Collection<String> expandAndCollect(String input) {
265
        if (isBlank(input)) return Collections.emptyList();
1✔
266

267
        if (input.contains(",")) {
×
268
            return Arrays.stream(input.split(","))
×
269
                .map(String::trim)
×
270
                .filter(StringUtils::isNotBlank)
×
271
                .collect(toList());
×
272
        }
273

274
        return Collections.singletonList(input.trim());
×
275
    }
276
}
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