Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: #887 Added WorkSpace class #888

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class CopyrightRangeProviderTest {
void copyrightRange() {
Map<String, String> props = new HashMap<>();
final LicenseCheckMojo mojo = new LicenseCheckMojo();
mojo.defaultBasedir = fsRepoRoot.toFile();
mojo.legacyDefaultBasedir = fsRepoRoot.toFile();
try (CopyrightRangeProvider provider = new CopyrightRangeProvider()) {
provider.init(mojo, props);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
import com.mycila.maven.plugin.license.AbstractLicenseMojo;
import com.mycila.maven.plugin.license.PropertiesProvider;
import com.mycila.maven.plugin.license.document.Document;
import com.mycila.maven.plugin.license.util.LazyMap;
import com.mycila.maven.plugin.license.util.Fn;
import com.mycila.maven.plugin.license.util.LazyMap;

import java.io.IOException;
import java.io.UncheckedIOException;
Expand All @@ -40,7 +40,7 @@ public class CopyrightAuthorProvider implements PropertiesProvider {

@Override
public void init(AbstractLicenseMojo mojo, Map<String, String> currentProperties) {
gitLookup = GitLookup.create(mojo.defaultBasedir, currentProperties);
gitLookup = GitLookup.create(mojo.legacyDefaultBasedir, currentProperties);
mathieu marked this conversation as resolved.
Show resolved Hide resolved

// One-time warning for shallow repo
if (mojo.warnIfShallow && gitLookup.isShallowRepository()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,16 @@
*/
package com.mycila.maven.plugin.license.git;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.Map;

import org.eclipse.jgit.api.errors.GitAPIException;

import com.mycila.maven.plugin.license.AbstractLicenseMojo;
import com.mycila.maven.plugin.license.PropertiesProvider;
import com.mycila.maven.plugin.license.document.Document;
import com.mycila.maven.plugin.license.util.LazyMap;
import com.mycila.maven.plugin.license.util.Fn;
import com.mycila.maven.plugin.license.util.LazyMap;
import org.eclipse.jgit.api.errors.GitAPIException;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.Map;

/**
* An implementation of {@link PropertiesProvider} that adds {@value #COPYRIGHT_LAST_YEAR_KEY} and
Expand All @@ -44,7 +43,7 @@ public class CopyrightRangeProvider implements PropertiesProvider {

@Override
public void init(AbstractLicenseMojo mojo, Map<String, String> currentProperties) {
gitLookup = GitLookup.create(mojo.defaultBasedir, currentProperties);
gitLookup = GitLookup.create(mojo.legacyDefaultBasedir, currentProperties);

// One-time warning for shallow repo
if (mojo.warnIfShallow && gitLookup.isShallowRepository()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class CopyrightAuthorProviderTest {
void copyrightAuthor() {
Map<String, String> props = new HashMap<>();
final LicenseCheckMojo mojo = new LicenseCheckMojo();
mojo.defaultBasedir = gitRepoRoot.toFile();
mojo.legacyDefaultBasedir = gitRepoRoot.toFile();

try (CopyrightAuthorProvider provider = new CopyrightAuthorProvider()) {
provider.init(mojo, props);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class CopyrightRangeProviderTest {
void copyrightRange() {
Map<String, String> props = new HashMap<>();
final LicenseCheckMojo mojo = new LicenseCheckMojo();
mojo.defaultBasedir = gitRepoRoot.toFile();
mojo.legacyDefaultBasedir = gitRepoRoot.toFile();
try (CopyrightRangeProvider provider = new CopyrightRangeProvider()) {
provider.init(mojo, props);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.mycila.maven.plugin.license.header.HeaderDefinition;
import com.mycila.maven.plugin.license.header.HeaderSource;
import com.mycila.maven.plugin.license.header.HeaderType;
import com.mycila.maven.plugin.license.util.FileUtils;
import com.mycila.maven.plugin.license.util.LazyMap;
import com.mycila.maven.plugin.license.util.Selection;
import com.mycila.maven.plugin.license.util.resource.ResourceFinder;
Expand Down Expand Up @@ -85,6 +86,10 @@ public abstract class AbstractLicenseMojo extends AbstractMojo {

private static final String[] DEFAULT_KEYWORDS = {"copyright"};

@Parameter(property = "license.workspace", alias = "workspace")
public WorkSpace workspace = new WorkSpace();


@Parameter(property = "license.licenseSets", alias = "licenseSets")
public LicenseSet[] licenseSets;

Expand All @@ -94,9 +99,12 @@ public abstract class AbstractLicenseMojo extends AbstractMojo {
* This is named `defaultBaseDirectory` as it will be used as the default
* value for the base directory. This default value can be overridden
* in each LicenseSet by setting {@link LicenseSet#basedir}.
*
* @deprecated use {@link WorkSpace#basedir}
*/
@Deprecated
@Parameter(property = "license.basedir", defaultValue = "${project.basedir}", alias = "basedir", required = true)
public File defaultBasedir;
public File legacyDefaultBasedir;
mathieu marked this conversation as resolved.
Show resolved Hide resolved

/**
* Location of the header. It can be a relative path, absolute path,
Expand Down Expand Up @@ -492,13 +500,29 @@ protected final void execute(final Callback callback) throws MojoExecutionExcept
}

// make default base dir canonical
this.defaultBasedir = this.getCanonicalFile(this.defaultBasedir, "license.basedir");
workspace.basedir = getCanonicalFile(firstNonNull(workspace.basedir, legacyDefaultBasedir), "license.workspace.basedir");
mathieu marked this conversation as resolved.
Show resolved Hide resolved

// collect all the license sets together
final LicenseSet[] allLicenseSets;

// if we abandon the legacy config this contiguous block can be removed
final LicenseSet legacyLicenseSet = convertLegacyConfigToLicenseSet();

if (workspace.basedir != null) {
mathieu marked this conversation as resolved.
Show resolved Hide resolved
if (legacyLicenseSet != null && legacyLicenseSet.basedir != null) {
if (!FileUtils.isSubfolder(legacyLicenseSet.basedir, workspace.basedir)) {
throw new MojoExecutionException("Legacy basedir parameter should be a subfolder of the workspace basedir.");
mathieu marked this conversation as resolved.
Show resolved Hide resolved
}
}
for (LicenseSet licenseSet : licenseSets) {
if (licenseSet.basedir != null) {
if (!FileUtils.isSubfolder(licenseSet.basedir, workspace.basedir)) {
throw new MojoExecutionException(String.format("LicenseSet basedir parameter [%s] should be a subfolder of the workspace basedir.", licenseSet.basedir.getPath()));
mathieu marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
}
mathieu marked this conversation as resolved.
Show resolved Hide resolved

if (legacyLicenseSet != null) {
if (licenseSets == null) {
allLicenseSets = new LicenseSet[]{legacyLicenseSet};
Expand Down Expand Up @@ -556,7 +580,8 @@ private void executeForLicenseSets(final LicenseSet[] licenseSets, final Callbac
}

private boolean detectLegacyUse() {
return legacyConfigHeader != null
return legacyDefaultBasedir != null
|| legacyConfigHeader != null
|| legacyConfigInlineHeader != null
|| (legacyConfigValidHeaders != null && legacyConfigValidHeaders.length > 0)
|| legacyConfigMulti != null
Expand All @@ -580,11 +605,12 @@ private LicenseSet convertLegacyConfigToLicenseSet() {
legacyLicenseSet.includes = legacyConfigIncludes;
legacyLicenseSet.excludes = legacyConfigExcludes;
legacyLicenseSet.keywords = legacyConfigKeywords;
legacyLicenseSet.basedir = legacyDefaultBasedir;
return legacyLicenseSet;
}

private void executeForLicenseSet(final LicenseSet licenseSet, final Callback callback) throws MojoExecutionException, MojoFailureException {
final ResourceFinder finder = new ResourceFinder(firstNonNull(asPath(licenseSet.basedir), asPath(defaultBasedir)));
final ResourceFinder finder = new ResourceFinder(firstNonNull(asPath(licenseSet.basedir), asPath(workspace.basedir)));
mathieu marked this conversation as resolved.
Show resolved Hide resolved
try {
finder.setCompileClassPath(project.getCompileClasspathElements());
} catch (DependencyResolutionRequiredException e) {
Expand Down Expand Up @@ -671,7 +697,7 @@ private void executeForLicenseSet(final LicenseSet licenseSet, final Callback ca
};

final DocumentFactory documentFactory = new DocumentFactory(
firstNonNull(licenseSet.basedir, defaultBasedir), buildMapping(),
firstNonNull(licenseSet.basedir, workspace.basedir), buildMapping(),
mathieu marked this conversation as resolved.
Show resolved Hide resolved
buildHeaderDefinitions(licenseSet, finder), Charset.forName(encoding), licenseSet.keywords,
perDocumentProperties);

Expand Down Expand Up @@ -786,9 +812,9 @@ private Map<String, String> getDefaultProperties() {
private String[] listSelectedFiles(final LicenseSet licenseSet) {
final boolean useDefaultExcludes = (licenseSet.useDefaultExcludes != null ? licenseSet.useDefaultExcludes : defaultUseDefaultExcludes);
final Selection selection = new Selection(
firstNonNull(licenseSet.basedir, defaultBasedir), licenseSet.includes, buildExcludes(licenseSet), useDefaultExcludes,
firstNonNull(licenseSet.basedir, workspace.basedir), licenseSet.includes, buildExcludes(licenseSet), useDefaultExcludes,
getLog());
debug("From: %s", firstNonNull(licenseSet.basedir, defaultBasedir));
debug("From: %s", firstNonNull(licenseSet.basedir, workspace.basedir));
mathieu marked this conversation as resolved.
Show resolved Hide resolved
debug("Including: %s", deepToString(selection.getIncluded()));
debug("Excluding: %s", deepToString(selection.getExcluded()));
return selection.getSelectedFiles();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
public class LicenseSet {

/**
* The base directory, in which to search for project files.
* The base directory, in which to search for project files. If {@link WokrSpace#basedir}
* is defined, then this base directory should be one of it's subfolders.
*/
@Parameter(property = "license.basedir")
public File basedir;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright (C) 2008-2024 Mycila ([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mycila.maven.plugin.license;

import org.apache.maven.plugins.annotations.Parameter;

import java.io.File;

public class WorkSpace {

/**
* The base directory, in which to search for project files.
* <p>
* It will be used as the default value for the base directory of each LicenseSet.
* This default value can be overridden in each LicenseSet by setting {@link LicenseSet#basedir}.
*/

@Parameter(property = "license.workspace.basedir", defaultValue = "${project.basedir}", alias = "basedir")
mathieu marked this conversation as resolved.
Show resolved Hide resolved
File basedir;

@Parameter
String[] includes = new String[0];

@Parameter
String[] excludes = new String[0];
mathieu marked this conversation as resolved.
Show resolved Hide resolved
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.UncheckedIOException;
import java.net.URL;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
Expand Down Expand Up @@ -133,4 +133,10 @@ public static void copyFilesToFolder(File src, File dst) {
}
});
}

public static boolean isSubfolder(File subfolder, File folder) {
String subfolderPath = subfolder.getAbsolutePath();
String folderPath = folder.getAbsolutePath();
return subfolderPath.startsWith(folderPath) && subfolderPath.length() > folderPath.length() && subfolderPath.charAt(folderPath.length()) == File.separatorChar;
}
mathieu marked this conversation as resolved.
Show resolved Hide resolved
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class AdditionalHeaderMojoTest {
@Test
void test_additionalHeaderDefinitions() throws Exception {
LicenseCheckMojo check = new LicenseCheckMojo();
check.defaultBasedir = new File("src/test/resources/check/def");
check.legacyDefaultBasedir = new File("src/test/resources/check/def");
check.legacyConfigHeader = "src/test/resources/check/header.txt";
check.project = new MavenProjectStub();
check.legacyConfigExcludes = new String[]{"*.xml"};
Expand All @@ -48,7 +48,7 @@ void test_additionalHeaderDefinitions() throws Exception {
@Test
void test_inline() throws Exception {
LicenseCheckMojo check = new LicenseCheckMojo();
check.defaultBasedir = new File("src/test/resources/check/def");
check.legacyDefaultBasedir = new File("src/test/resources/check/def");
check.legacyConfigHeader = "src/test/resources/check/header.txt";
check.project = new MavenProjectStub();
check.legacyConfigExcludes = new String[]{"*.xml"};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public List<String> getModules() {
}
};
LicenseCheckMojo check = new LicenseCheckMojo();
check.defaultBasedir = new File("src/test/resources/check/modules");
check.legacyDefaultBasedir = new File("src/test/resources/check/modules");
check.legacyConfigHeader = "header.txt";
check.project = project;
check.strictCheck = true;
Expand All @@ -51,7 +51,7 @@ public List<String> getModules() {
};
LicenseCheckMojo check = new LicenseCheckMojo();
check.project = project;
check.defaultBasedir = new File("src/test/resources/check/modules");
check.legacyDefaultBasedir = new File("src/test/resources/check/modules");
check.legacyConfigHeader = "header.txt";
check.aggregate = true;
check.strictCheck = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ void test_line_wrapping() throws Exception {
MavenProjectStub project = new MavenProjectStub();

LicenseCheckMojo check = new LicenseCheckMojo();
check.defaultBasedir = new File("src/test/resources/check/linewrap");
check.legacyDefaultBasedir = new File("src/test/resources/check/linewrap");
check.legacyConfigHeader = "header.txt";
check.project = project;

Expand All @@ -59,15 +59,15 @@ void test_line_wrapping() throws Exception {
FileUtils.copyFileToFolder(new File("src/test/resources/check/linewrap/testconfig.xml"), tmp);

LicenseFormatMojo updater = new LicenseFormatMojo();
updater.defaultBasedir = tmp;
updater.legacyDefaultBasedir = tmp;
updater.legacyConfigHeader = "src/test/resources/check/linewrap/header.txt";
updater.project = project;
updater.strictCheck = true;
updater.execute();

// the check again, strictly. should work now
check = new LicenseCheckMojo();
check.defaultBasedir = tmp;
check.legacyDefaultBasedir = tmp;
check.legacyConfigHeader = "src/test/resources/check/linewrap/header.txt";
check.project = project;

Expand Down
Loading
Loading