• 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

65.06
/plugins/jreleaser-maven-plugin/src/main/java/org/jreleaser/maven/plugin/AbstractJReleaserMojo.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.maven.plugin;
19

20
import org.apache.maven.execution.MavenSession;
21
import org.apache.maven.plugin.AbstractMojo;
22
import org.apache.maven.plugin.MojoExecutionException;
23
import org.apache.maven.plugin.MojoFailureException;
24
import org.apache.maven.plugins.annotations.Parameter;
25
import org.apache.maven.project.MavenProject;
26
import org.jreleaser.engine.context.ContextCreator;
27
import org.jreleaser.logging.JReleaserLogger;
28
import org.jreleaser.maven.plugin.internal.JReleaserLoggerAdapter;
29
import org.jreleaser.maven.plugin.internal.JReleaserModelConfigurer;
30
import org.jreleaser.model.JReleaserException;
31
import org.jreleaser.model.JReleaserVersion;
32
import org.jreleaser.model.api.JReleaserCommand;
33
import org.jreleaser.model.api.JReleaserContext.Mode;
34
import org.jreleaser.model.internal.JReleaserContext;
35
import org.jreleaser.model.internal.JReleaserModel;
36
import org.jreleaser.util.Env;
37
import org.jreleaser.util.PlatformUtils;
38
import org.jreleaser.util.StringUtils;
39

40
import java.io.File;
41
import java.io.FileOutputStream;
42
import java.io.IOException;
43
import java.io.PrintWriter;
44
import java.nio.file.Path;
45
import java.nio.file.Paths;
46
import java.util.ArrayList;
47
import java.util.Arrays;
48
import java.util.Collections;
49
import java.util.List;
50
import java.util.Locale;
51

52
import static java.util.stream.Collectors.toList;
53
import static org.jreleaser.model.JReleaserOutput.JRELEASER_QUIET;
54
import static org.jreleaser.util.IoUtils.newPrintWriter;
55
import static org.jreleaser.util.StringUtils.isBlank;
56
import static org.jreleaser.util.StringUtils.isNotBlank;
57

58
/**
59
 * @author Andres Almiray
60
 * @since 0.1.0
61
 */
62
abstract class AbstractJReleaserMojo extends AbstractMojo {
1✔
63
    /**
64
     * The project whose model will be checked.
65
     */
66
    @Parameter(defaultValue = "${project}", readonly = true, required = true)
67
    protected MavenProject project;
68

69
    @Parameter
70
    protected JReleaserModel jreleaser;
71

72
    @Parameter(property = "jreleaser.output.directory", defaultValue = "${project.build.directory}/jreleaser")
73
    protected File outputDirectory;
74

75
    @Parameter(property = "jreleaser.config.file")
76
    protected File configFile;
77

78
    /**
79
     * Skips remote operations.
80
     */
81
    @Parameter(property = "jreleaser.dry.run")
82
    protected Boolean dryrun;
83

84
    /**
85
     * Searches for the Git root.
86
     */
87
    @Parameter(property = "jreleaser.git.root.search")
88
    protected Boolean gitRootSearch;
89

90
    /**
91
     * Enable strict mode.
92
     */
93
    @Parameter(property = "jreleaser.strict")
94
    protected Boolean strict;
95

96
    @Parameter(defaultValue = "${session}", required = true)
97
    private MavenSession session;
98

99
    @Parameter(defaultValue = "${maven.multiModuleProjectDirectory}")
100
    private String multiModuleProjectDirectory;
101

102
    @Override
103
    public void execute() throws MojoExecutionException, MojoFailureException {
104
        Banner.display(project, getLog());
1✔
105
        if (isSkip()) {
1✔
106
            getLog().info("Execution has been explicitly skipped.");
×
107
            return;
×
108
        }
109

110
        doExecute();
1✔
111
    }
1✔
112

113
    protected abstract boolean isSkip();
114

115
    protected abstract void doExecute() throws MojoExecutionException, MojoFailureException;
116

117
    protected boolean isQuiet() {
118
        return getLog().isErrorEnabled() &&
1✔
119
            !getLog().isWarnEnabled() &&
1✔
120
            !getLog().isInfoEnabled() &&
×
121
            !getLog().isDebugEnabled();
1✔
122
    }
123

124
    protected JReleaserLogger getLogger() throws MojoExecutionException {
125
        return new JReleaserLoggerAdapter(createTracer(), getLog());
1✔
126
    }
127

128
    protected PrintWriter createTracer() throws MojoExecutionException {
129
        try {
130
            java.nio.file.Files.createDirectories(outputDirectory.toPath());
1✔
131
            return newPrintWriter(new FileOutputStream(
1✔
132
                outputDirectory.toPath().resolve("trace.log").toFile()));
1✔
133
        } catch (IOException e) {
×
134
            throw new MojoExecutionException("Could not initialize trace file", e);
×
135
        }
136
    }
137

138
    protected JReleaserModel convertModel() {
139
        JReleaserModel jreleaserModel = null != jreleaser ? jreleaser : new JReleaserModel();
×
140
        return JReleaserModelConfigurer.configure(jreleaserModel, project, session);
×
141
    }
142

143
    protected JReleaserModel readModel(JReleaserLogger logger) {
144
        JReleaserModel jreleaserModel = ContextCreator.resolveModel(logger, configFile.toPath());
1✔
145
        return JReleaserModelConfigurer.configure(jreleaserModel, project, session);
1✔
146
    }
147

148
    protected JReleaserContext createContext() throws MojoExecutionException {
149
        try {
150
            if (isQuiet()) {
1✔
151
                System.setProperty(JRELEASER_QUIET, "true");
×
152
            }
153

154
            JReleaserLogger logger = getLogger();
1✔
155
            PlatformUtils.resolveCurrentPlatform(logger);
1✔
156
            Path basedir = resolveBasedir();
1✔
157

158
            logger.info("JReleaser {}", JReleaserVersion.getPlainVersion());
1✔
159
            JReleaserVersion.banner(logger.getTracer());
1✔
160
            if (null != configFile) {
1✔
161
                logger.info("Configuring with {}", configFile.getAbsolutePath());
1✔
162
            }
163
            logger.increaseIndent();
1✔
164
            logger.info("- basedir set to {}", basedir.toAbsolutePath());
1✔
165
            logger.info("- outputdir set to {}", outputDirectory.toPath().toAbsolutePath());
1✔
166
            logger.decreaseIndent();
1✔
167

168
            return ContextCreator.create(
1✔
169
                logger,
170
                resolveConfigurer(configFile),
1✔
171
                getMode(),
1✔
172
                getCommand(),
1✔
173
                null == configFile ? convertModel() : readModel(logger),
1✔
174
                basedir,
175
                outputDirectory.toPath(),
1✔
176
                resolveBoolean(org.jreleaser.model.api.JReleaserContext.DRY_RUN, dryrun),
1✔
177
                resolveBoolean(org.jreleaser.model.api.JReleaserContext.GIT_ROOT_SEARCH, gitRootSearch),
1✔
178
                resolveBoolean(org.jreleaser.model.api.JReleaserContext.STRICT, strict),
1✔
179
                collectSelectedPlatforms(),
1✔
180
                collectRejectedPlatforms());
1✔
181
        } catch (JReleaserException e) {
×
182
            throw new MojoExecutionException("JReleaser for project " + project.getArtifactId() + " has not been properly configured.", e);
×
183
        }
184
    }
185

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

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

202
    protected JReleaserContext.Configurer resolveConfigurer(File configFile) {
203
        if (null == configFile) return JReleaserContext.Configurer.MAVEN;
1✔
204

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

219
    protected Mode getMode() {
220
        return Mode.FULL;
×
221
    }
222

223
    protected abstract JReleaserCommand getCommand();
224

225
    private Path resolveBasedir() {
226
        String resolvedBasedir = Env.resolve(org.jreleaser.model.api.JReleaserContext.BASEDIR, "");
1✔
227
        if (isNotBlank(resolvedBasedir)) {
1✔
228
            return Paths.get(resolvedBasedir.trim());
×
229
        } else if (isNotBlank(multiModuleProjectDirectory)) {
1✔
230
            return Paths.get(multiModuleProjectDirectory.trim());
1✔
231
        } else if (isNotBlank(session.getExecutionRootDirectory())) {
×
232
            return Paths.get(session.getExecutionRootDirectory().trim());
×
233
        }
234
        return project.getBasedir().toPath();
×
235
    }
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(String[] input) {
246
        return collectEntries(input, false);
1✔
247
    }
248

249
    protected List<String> collectEntries(String[] input, boolean lowerCase) {
250
        List<String> list = new ArrayList<>();
1✔
251
        if (null != input && input.length > 0) {
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
}
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