Skip to content

Commit

Permalink
Add option to prohibit the use of legacy configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
teabot authored and hazendaz committed Aug 15, 2023
1 parent d9165f3 commit d22d27d
Show file tree
Hide file tree
Showing 3 changed files with 170 additions and 2 deletions.
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ The table below shows all the available options you can use in the configure sec
All plugin configuration options are described in the [Detailed Maven documentation](#detailed-maven-documentation) but here are some details.

- `useDefaultExcludes`: The default exclusion list can be found [here](https://github.com/mathieucarbou/license-maven-plugin/blob/master/license-maven-plugin/src/main/java/com/mycila/maven/plugin/license/Default.java)

- `prohibitLegacyUse`: Fail when deprecated configuration options are used (the default behaviour is to warn).

### License templates

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@
*/
public abstract class AbstractLicenseMojo extends AbstractMojo {

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

@Parameter
public LicenseSet[] licenseSets;

Expand Down Expand Up @@ -228,7 +230,7 @@ public abstract class AbstractLicenseMojo extends AbstractMojo {
*/
@Deprecated
@Parameter(alias = "keywords")
public String[] legacyConfigKeywords = new String[]{"copyright"};
public String[] legacyConfigKeywords = DEFAULT_KEYWORDS;

/**
* Specify if you want to use default exclusions besides the files you have
Expand Down Expand Up @@ -454,6 +456,9 @@ public abstract class AbstractLicenseMojo extends AbstractMojo {
@Parameter(property = "license.report.skip", defaultValue = "false")
public boolean reportSkipped = false;

@Parameter(property = "license.prohibitLegacyUse", defaultValue = "false")
public boolean prohibitLegacyUse = false;

protected Clock clock = Clock.systemUTC();
protected Report report;

Expand Down Expand Up @@ -487,6 +492,9 @@ public void checkUnknown() throws MojoExecutionException {
@SuppressWarnings({"unchecked"})
protected final void execute(final Callback callback) throws MojoExecutionException, MojoFailureException {
if (!skip) {
if (prohibitLegacyUse && detectLegacyUse()) {
throw new MojoExecutionException("Use of legacy parameters has been prohibited by configuration.");
}

// make default base dir canonical
this.defaultBasedir = this.getCanonicalFile(this.defaultBasedir, "license.basedir");
Expand Down Expand Up @@ -552,6 +560,17 @@ private void executeForLicenseSets(final LicenseSet[] licenseSets, final Callbac
}
}

private boolean detectLegacyUse() {
return legacyConfigHeader != null
|| legacyConfigInlineHeader != null
|| (legacyConfigValidHeaders != null && legacyConfigValidHeaders.length > 0)
|| legacyConfigMulti != null
|| (legacyConfigHeaderSections != null && legacyConfigHeaderSections.length > 0)
|| (legacyConfigIncludes != null && legacyConfigIncludes.length > 0)
|| (legacyConfigExcludes != null && legacyConfigExcludes.length > 0)
|| (legacyConfigKeywords != null && !Arrays.equals(legacyConfigKeywords, DEFAULT_KEYWORDS));
}

private LicenseSet convertLegacyConfigToLicenseSet() {
if (legacyConfigHeader == null && (this.legacyConfigInlineHeader == null || this.legacyConfigInlineHeader.isEmpty())) {
return null;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
/**
* Copyright (C) 2008 Mycila ([email protected])
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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.plugin.MojoExecutionException;
import org.apache.maven.plugin.testing.stubs.MavenProjectStub;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatNoException;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

@SuppressWarnings("deprecation")
public class ProhibitLegacyUseTest {

@Test
void test_defaultStubProject() throws Exception {
LicenseCheckMojo check = new LicenseCheckMojo();
check.project = new MavenProjectStub();
check.prohibitLegacyUse = true;
check.execute();
}

@Test
void test_prohibitLegacyConfigHeader() {
LicenseCheckMojo check = new LicenseCheckMojo();
check.project = new MavenProjectStub();
check.prohibitLegacyUse = true;
check.legacyConfigHeader = "header";
assertThatThrownBy(check::execute).satisfies(e -> {
assertThat(e).isInstanceOf(MojoExecutionException.class);
assertThat(e).hasMessage("Use of legacy parameters has been prohibited by configuration.");
}
);
}

@Test
void test_prohibitLegacyConfigInlineHeader() {
LicenseCheckMojo check = new LicenseCheckMojo();
check.project = new MavenProjectStub();
check.prohibitLegacyUse = true;
check.legacyConfigInlineHeader = "inline header";
assertThatThrownBy(check::execute).satisfies(e -> {
assertThat(e).isInstanceOf(MojoExecutionException.class);
assertThat(e).hasMessage("Use of legacy parameters has been prohibited by configuration.");
}
);
}

@Test
void test_prohibitLegacyConfigIncludes() {
LicenseCheckMojo check = new LicenseCheckMojo();
check.project = new MavenProjectStub();
check.prohibitLegacyUse = true;
check.legacyConfigIncludes = new String[]{"include"};
assertThatThrownBy(check::execute).satisfies(e -> {
assertThat(e).isInstanceOf(MojoExecutionException.class);
assertThat(e).hasMessage("Use of legacy parameters has been prohibited by configuration.");
}
);
}

@Test
void test_prohibitLegacyConfigExcludes() {
LicenseCheckMojo check = new LicenseCheckMojo();
check.project = new MavenProjectStub();
check.prohibitLegacyUse = true;
check.legacyConfigExcludes = new String[]{"exclude"};
assertThatThrownBy(check::execute).satisfies(e -> {
assertThat(e).isInstanceOf(MojoExecutionException.class);
assertThat(e).hasMessage("Use of legacy parameters has been prohibited by configuration.");
}
);
}

@Test
void test_prohibitLegacyConfigKeywords() {
LicenseCheckMojo check = new LicenseCheckMojo();
check.project = new MavenProjectStub();
check.prohibitLegacyUse = true;
check.legacyConfigKeywords = new String[]{"keyword"};
assertThatThrownBy(check::execute).satisfies(e -> {
assertThat(e).isInstanceOf(MojoExecutionException.class);
assertThat(e).hasMessage("Use of legacy parameters has been prohibited by configuration.");
}
);
}

@Test
void test_allowDefaultLegacyConfigKeywords() {
LicenseCheckMojo check = new LicenseCheckMojo();
check.project = new MavenProjectStub();
check.prohibitLegacyUse = true;
check.legacyConfigKeywords = new String[]{"copyright"};
assertThatNoException().isThrownBy(check::execute);
}

@Test
void test_prohibitLegacyConfigMulti() {
LicenseCheckMojo check = new LicenseCheckMojo();
check.project = new MavenProjectStub();
check.prohibitLegacyUse = true;
check.legacyConfigMulti = new Multi();
assertThatThrownBy(check::execute).satisfies(e -> {
assertThat(e).isInstanceOf(MojoExecutionException.class);
assertThat(e).hasMessage("Use of legacy parameters has been prohibited by configuration.");
}
);
}

@Test
void test_prohibitLegacyConfigValidHeaders() {
LicenseCheckMojo check = new LicenseCheckMojo();
check.project = new MavenProjectStub();
check.prohibitLegacyUse = true;
check.legacyConfigValidHeaders = new String[]{"valid", "headers"};
assertThatThrownBy(check::execute).satisfies(e -> {
assertThat(e).isInstanceOf(MojoExecutionException.class);
assertThat(e).hasMessage("Use of legacy parameters has been prohibited by configuration.");
}
);
}

@Test
void test_prohibitLegacyConfigValidHeaderSections() {
LicenseCheckMojo check = new LicenseCheckMojo();
check.project = new MavenProjectStub();
check.prohibitLegacyUse = true;
check.legacyConfigHeaderSections = new HeaderSection[]{new HeaderSection()};
assertThatThrownBy(check::execute).satisfies(e -> {
assertThat(e).isInstanceOf(MojoExecutionException.class);
assertThat(e).hasMessage("Use of legacy parameters has been prohibited by configuration.");
}
);
}
}

0 comments on commit d22d27d

Please sign in to comment.