forked from mojohaus/license-maven-plugin
/
LicenseMojoUtils.java
136 lines (121 loc) · 4.53 KB
/
LicenseMojoUtils.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package org.codehaus.mojo.license;
/*
* #%L
* License Maven Plugin
* %%
* Copyright (C) 2018 Codehaus
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Lesser Public License for more details.
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/lgpl-3.0.html>.
* #L%
*/
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import org.apache.maven.plugin.logging.Log;
import org.codehaus.mojo.license.utils.UrlRequester;
/**
* Utility methods common to various mojos.
*
* @since 1.17
*/
public final class LicenseMojoUtils
{
/** A special singleton to pass the information that the URL was not set. */
static final String NO_URL = "file:///inexistent";
static final String DEFAULT_OVERRIDE_THIRD_PARTY = "src/license/override-THIRD-PARTY.properties";
/**
* Hidden
*/
private LicenseMojoUtils()
{
}
/**
* @param url the {@link URL} to check
* @return {@code url != null && url != NO_URL}
*/
public static boolean isValid( String url )
{
return url != null && !NO_URL.equals( url );
}
/**
* Chooses the override {@link URL} to use out of {@code resolvedUrl}, {@code deprecatedFile}, {@code url}, or the
* default given by {@link #DEFAULT_OVERRIDE_THIRD_PARTY}.
*
* @param resolvedUrl returns this one if it is not {@code null} and not equal to {@link #NO_URL}
* @param deprecatedFile the deprecated mojo parameter
* @param url the newer variant of the mojo parameter
* @param basedir {@code basedir} to resolve {@value #DEFAULT_OVERRIDE_THIRD_PARTY} against
* @return a valid URL or {@link #NO_URL}, never {@code null}
*/
static String prepareThirdPartyOverrideUrl( final String resolvedUrl, final File deprecatedFile, final String url,
File basedir, Log log )
{
if ( deprecatedFile != null )
{
log.warn( "'overrideFile' mojo parameter is deprecated. Use 'overrideUrl' instead." );
}
return prepareUrl( resolvedUrl, deprecatedFile, url, basedir, DEFAULT_OVERRIDE_THIRD_PARTY, log );
}
private static String prepareUrl( final String resolvedUrl, final File deprecatedFile, final String url,
File basedir, String defaultFilePath, Log log )
{
if ( resolvedUrl != null && !NO_URL.equals( resolvedUrl ) )
{
return resolvedUrl;
}
if ( deprecatedFile != null && url != null && !url.isEmpty() )
{
throw new IllegalArgumentException( "You can't use both overrideFile and overrideUrl" );
}
if ( deprecatedFile != null )
{
if ( deprecatedFile.exists() )
{
String result = deprecatedFile.toURI().toString();
log.debug( "Loading overrides from file " + result );
return result;
}
else
{
log.warn( "overrideFile [" + deprecatedFile.getAbsolutePath() + "] was configured but doesn't exist" );
}
}
if ( url != null )
{
if ( UrlRequester.isStringUrl( url ) )
{
log.debug( "Loading overrides from URL " + url );
return url;
}
else
{
log.warn( "Unsupported or invalid URL [" + url + "] found in overrideUrl; "
+ "supported are 'classpath:' URLs and anything your JVM supports "
+ "(file:, http: and https: should always work)" );
}
}
final Path basedirPath = basedir.toPath();
final Path defaultPath = basedirPath.resolve( defaultFilePath );
if ( Files.exists( defaultPath ) )
{
String result = defaultPath.toUri().toString();
log.debug( "Loading overrides from file " + result );
return result;
}
log.debug( "No (valid) URL and no file [" + defaultPath.toAbsolutePath()
+ "] found; not loading any overrides" );
return NO_URL;
}
}