Skip to content

Commit

Permalink
Replace deprecated Component annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
slawekjaranowski committed Nov 29, 2024
1 parent 10a9053 commit b572848
Show file tree
Hide file tree
Showing 19 changed files with 174 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
import org.apache.maven.artifact.Artifact;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.ProjectBuildingException;
Expand Down Expand Up @@ -600,17 +599,15 @@ public abstract class AbstractAddThirdPartyMojo extends AbstractLicenseMojo {
*
* @since 1.0
*/
@Component
ThirdPartyTool thirdPartyTool;
final ThirdPartyTool thirdPartyTool;

/**
* Dependencies tool. (pluggable component to find dependencies that match up with
* criteria).
*
* @since 1.1
*/
@Component
DependenciesTool dependenciesTool;
final DependenciesTool dependenciesTool;

// ----------------------------------------------------------------------
// Private fields
Expand Down Expand Up @@ -669,6 +666,11 @@ public abstract class AbstractAddThirdPartyMojo extends AbstractLicenseMojo {
@Parameter(property = "license.artifactFiltersUrl")
protected String artifactFiltersUrl;

protected AbstractAddThirdPartyMojo(ThirdPartyTool thirdPartyTool, DependenciesTool dependenciesTool) {
this.thirdPartyTool = thirdPartyTool;
this.dependenciesTool = dependenciesTool;
}

// ----------------------------------------------------------------------
// Abstract Methods
// ----------------------------------------------------------------------
Expand Down Expand Up @@ -860,7 +862,7 @@ void resolveUnsafeDependenciesFromArtifact(String groupId, String artifactId, St
resolveUnsafeDependenciesFromFile(missingLicensesFromArtifact);
}

void resolveUnsafeDependenciesFromFile(File missingLicenses) throws IOException, MojoExecutionException {
void resolveUnsafeDependenciesFromFile(File missingLicenses) throws IOException {
if (missingLicenses == null) {
return;
}
Expand Down Expand Up @@ -897,18 +899,16 @@ void resolveUnsafeDependenciesFromFile(File missingLicenses) throws IOException,
}

void checkUnsafeDependencies() {
if (CollectionUtils.isNotEmpty(unsafeDependencies)) {
if (LOG.isWarnEnabled()) {
boolean plural = unsafeDependencies.size() > 1;
String message = String.format(
"There %s %d %s with no license :",
plural ? "are" : "is", unsafeDependencies.size(), plural ? "dependencies" : "dependency");
LOG.warn(message);
for (MavenProject dep : unsafeDependencies) {

// no license found for the dependency
LOG.warn(" - {}", MojoHelper.getArtifactId(dep.getArtifact()));
}
if (CollectionUtils.isNotEmpty(unsafeDependencies) && LOG.isWarnEnabled()) {
boolean plural = unsafeDependencies.size() > 1;
String message = String.format(
"There %s %d %s with no license :",
plural ? "are" : "is", unsafeDependencies.size(), plural ? "dependencies" : "dependency");
LOG.warn(message);
for (MavenProject dep : unsafeDependencies) {

// no license found for the dependency
LOG.warn(" - {}", MojoHelper.getArtifactId(dep.getArtifact()));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
import org.codehaus.mojo.license.download.LicenseMatchers;
import org.codehaus.mojo.license.download.LicenseSummaryReader;
import org.codehaus.mojo.license.download.LicensedArtifact;
import org.codehaus.mojo.license.download.LicensedArtifactResolver;
import org.codehaus.mojo.license.download.PreferredFileNames;
import org.codehaus.mojo.license.download.ProjectLicense;
import org.codehaus.mojo.license.download.ProjectLicenseInfo;
Expand Down Expand Up @@ -778,6 +779,10 @@ public abstract class AbstractDownloadLicensesMojo extends AbstractLicensesXmlMo

private UrlReplacements urlReplacements;

protected AbstractDownloadLicensesMojo(LicensedArtifactResolver licensedArtifactResolver) {
super(licensedArtifactResolver);
}

protected abstract boolean isSkip();

protected MavenProject getProject() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Parameter;
import org.codehaus.mojo.license.api.FreeMarkerHelper;
import org.codehaus.mojo.license.header.FileHeader;
Expand Down Expand Up @@ -328,8 +327,7 @@ public abstract class AbstractFileHeaderMojo extends AbstractLicenseNameMojo {
*
* @since 1.0
*/
@Component(role = FileHeaderTransformer.class)
private Map<String, FileHeaderTransformer> transformers;
private final Map<String, FileHeaderTransformer> transformers;

// ----------------------------------------------------------------------
// Private fields
Expand Down Expand Up @@ -376,6 +374,10 @@ public abstract class AbstractFileHeaderMojo extends AbstractLicenseNameMojo {
*/
private FreeMarkerHelper freeMarkerHelper = FreeMarkerHelper.newDefaultHelper();

protected AbstractFileHeaderMojo(Map<String, FileHeaderTransformer> transformers) {
this.transformers = transformers;
}

// ----------------------------------------------------------------------
// Abstract Methods
// ----------------------------------------------------------------------
Expand Down
15 changes: 6 additions & 9 deletions src/main/java/org/codehaus/mojo/license/AbstractLicenseMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/

import java.io.File;
import java.nio.charset.Charset;

import org.apache.commons.lang3.StringUtils;
import org.apache.maven.plugin.AbstractMojo;
Expand All @@ -31,7 +32,6 @@
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.codehaus.mojo.license.utils.MojoHelper;
import org.codehaus.plexus.util.ReaderFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -150,9 +150,7 @@ public final void execute() throws MojoExecutionException, MojoFailureException

init();

} catch (MojoFailureException e) {
throw e;
} catch (MojoExecutionException e) {
} catch (MojoFailureException | MojoExecutionException e) {
throw e;
} catch (Exception e) {
throw new MojoExecutionException(
Expand All @@ -169,9 +167,7 @@ public final void execute() throws MojoExecutionException, MojoFailureException

try {
doAction();
} catch (MojoFailureException e) {
throw e;
} catch (MojoExecutionException e) {
} catch (MojoFailureException | MojoExecutionException e) {
throw e;
} catch (Exception e) {
throw new MojoExecutionException(
Expand Down Expand Up @@ -320,10 +316,11 @@ protected void checkEncoding() {
LOG.info("Will check encoding: {}", getEncoding());
}
if (StringUtils.isEmpty(getEncoding())) {
String sysEncoding = Charset.defaultCharset().displayName();
LOG.warn(
"File encoding has not been set, using platform encoding {}, i.e. build is platform dependent!",
ReaderFactory.FILE_ENCODING);
setEncoding(ReaderFactory.FILE_ENCODING);
sysEncoding);
setEncoding(sysEncoding);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@

import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.codehaus.mojo.license.download.LicenseSummaryWriter;
Expand Down Expand Up @@ -106,11 +105,14 @@ public abstract class AbstractLicensesXmlMojo extends AbstractMojo {
*
* @since 1.0
*/
@Component
protected LicensedArtifactResolver licensedArtifactResolver;
protected final LicensedArtifactResolver licensedArtifactResolver;

private Charset charset;

protected AbstractLicensesXmlMojo(LicensedArtifactResolver licensedArtifactResolver) {
this.licensedArtifactResolver = licensedArtifactResolver;
}

/** {@inheritDoc} */
public String getEncoding() {
initEncoding();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
import org.apache.maven.artifact.resolver.ArtifactResolutionException;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.ProjectBuildingException;
Expand Down Expand Up @@ -227,9 +226,9 @@ public abstract class AbstractThirdPartyReport extends AbstractMavenReport
private String overrideUrl;

/**
* A {@link URL} prepared either our of {@link #overrideFile} or {@link #overrideUrl} or the default value.
* A URL prepared either our of {@link #overrideFile} or {@link #overrideUrl} or the default value.
*
* @see LicenseMojoUtils#prepareThirdPartyOverrideUrl(URL, File, String, File)
* @see LicenseMojoUtils#prepareThirdPartyOverrideUrl(String, File, String, File)
*/
String resolvedOverrideUrl;

Expand Down Expand Up @@ -300,24 +299,21 @@ public abstract class AbstractThirdPartyReport extends AbstractMavenReport
*
* @since 1.1
*/
@Component
private I18N i18n;
private final I18N i18n;

/**
* dependencies tool.
*
* @since 1.1
*/
@Component
private DependenciesTool dependenciesTool;
private final DependenciesTool dependenciesTool;

/**
* third party tool.
*
* @since 1.1
*/
@Component
private ThirdPartyTool thirdPartyTool;
private final ThirdPartyTool thirdPartyTool;

/**
* A URL returning a plain text file that contains include/exclude artifact filters in the following format:
Expand All @@ -341,6 +337,12 @@ public abstract class AbstractThirdPartyReport extends AbstractMavenReport

private ArtifactFilters artifactFilters;

protected AbstractThirdPartyReport(I18N i18n, DependenciesTool dependenciesTool, ThirdPartyTool thirdPartyTool) {
this.i18n = i18n;
this.dependenciesTool = dependenciesTool;
this.thirdPartyTool = thirdPartyTool;
}

// ----------------------------------------------------------------------
// Protected Abstract Methods
// ----------------------------------------------------------------------
Expand Down Expand Up @@ -396,7 +398,7 @@ protected void executeReport(Locale locale) throws MavenReportException {
}

ThirdPartyReportRenderer renderer =
new ThirdPartyReportRenderer(getSink(), i18n, getOutputName(), locale, details);
new ThirdPartyReportRenderer(getSink(), i18n, getOutputPath(), locale, details);
renderer.render();
}

Expand All @@ -405,15 +407,15 @@ protected void executeReport(Locale locale) throws MavenReportException {
*/
@Override
public String getDescription(Locale locale) {
return i18n.getString(getOutputName(), locale, "report.description");
return i18n.getString(getOutputPath(), locale, "report.description");
}

/**
* {@inheritDoc}
*/
@Override
public String getName(Locale locale) {
return i18n.getString(getOutputName(), locale, "report.title");
return i18n.getString(getOutputPath(), locale, "report.title");
}

// ----------------------------------------------------------------------
Expand Down
18 changes: 11 additions & 7 deletions src/main/java/org/codehaus/mojo/license/AddThirdPartyMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
* #L%
*/

import javax.inject.Inject;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
Expand All @@ -43,9 +45,11 @@
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.ProjectBuildingException;
import org.codehaus.mojo.license.api.ArtifactFilters;
import org.codehaus.mojo.license.api.DependenciesTool;
import org.codehaus.mojo.license.api.DependenciesToolException;
import org.codehaus.mojo.license.api.MavenProjectDependenciesConfigurator;
import org.codehaus.mojo.license.api.ResolvedProjectDependencies;
import org.codehaus.mojo.license.api.ThirdPartyTool;
import org.codehaus.mojo.license.api.ThirdPartyToolException;
import org.codehaus.mojo.license.model.LicenseMap;
import org.codehaus.mojo.license.utils.MojoHelper;
Expand Down Expand Up @@ -121,6 +125,11 @@ public class AddThirdPartyMojo extends AbstractAddThirdPartyMojo implements Mave

private ArtifactFilters artifactFilters;

@Inject
public AddThirdPartyMojo(ThirdPartyTool thirdPartyTool, DependenciesTool dependenciesTool) {
super(thirdPartyTool, dependenciesTool);
}

// ----------------------------------------------------------------------
// AbstractLicenseMojo Implementaton
// ----------------------------------------------------------------------
Expand Down Expand Up @@ -354,7 +363,7 @@ private boolean computeDoGenerateMissingFile(
}

/**
* Write the missing file ({@link #getMissingFile()}.
* Write the missing file.
*
* @throws IOException if error while writing missing file
*/
Expand All @@ -363,8 +372,7 @@ private void writeMissingFile() throws IOException {
Files.createDirectories(missingFile.getParentFile().toPath());
LOG.info("Regenerate missing license file {}", missingFile);

FileOutputStream writer = new FileOutputStream(missingFile);
try {
try (FileOutputStream writer = new FileOutputStream(missingFile)) {
StringBuilder sb = new StringBuilder(" Generated by " + getClass().getName());
List<String> licenses = new ArrayList<>(licenseMap.keySet());
licenses.remove(LicenseMap.UNKNOWN_LICENSE_MESSAGE);
Expand All @@ -378,8 +386,6 @@ private void writeMissingFile() throws IOException {
sb.append("\n-------------------------------------------------------------------------------");
sb.append("\n Please fill the missing licenses for dependencies :\n\n");
unsafeMappings.store(writer, sb.toString());
} finally {
writer.close();
}
}

Expand Down Expand Up @@ -420,8 +426,6 @@ void initFromMojo(AggregatorAddThirdPartyMojo mojo, MavenProject mavenProject) t
fileTemplate = mojo.fileTemplate;
verbose = mojo.verbose;
encoding = mojo.encoding;
thirdPartyTool = mojo.thirdPartyTool;
dependenciesTool = mojo.dependenciesTool;

setLog(mojo.getLog());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
* #L%
*/

import javax.inject.Inject;

import java.util.List;
import java.util.Map;
import java.util.TreeMap;
Expand All @@ -33,6 +35,7 @@
import org.apache.maven.project.MavenProject;
import org.codehaus.mojo.license.api.ResolvedProjectDependencies;
import org.codehaus.mojo.license.download.LicensedArtifact;
import org.codehaus.mojo.license.download.LicensedArtifactResolver;
import org.codehaus.mojo.license.utils.MojoHelper;

/**
Expand Down Expand Up @@ -108,6 +111,11 @@ public class AggregateDownloadLicensesMojo extends AbstractDownloadLicensesMojo
@Parameter(property = "license.extendedInfo", defaultValue = "false")
private boolean extendedInfo;

@Inject
public AggregateDownloadLicensesMojo(LicensedArtifactResolver licensedArtifactResolver) {
super(licensedArtifactResolver);
}

// ----------------------------------------------------------------------
// AbstractDownloadLicensesMojo Implementation
// ----------------------------------------------------------------------
Expand Down
Loading

0 comments on commit b572848

Please sign in to comment.