This is an automated email from the ASF dual-hosted git repository. slachiewicz pushed a commit to branch MANTRUN-239 in repository https://gitbox.apache.org/repos/asf/maven-antrun-plugin.git
commit 739481086edfdaf1868d0600ce341fb5225dcc1e Author: Sylwester Lachiewicz <slachiew...@apache.org> AuthorDate: Thu May 25 22:29:10 2023 +0200 [MANTRUN-239] Reformat with spotless plugin --- pom.xml | 46 +-- .../apache/maven/ant/tasks/AttachArtifactTask.java | 83 ++--- .../maven/ant/tasks/DependencyFilesetsTask.java | 128 +++---- .../support/SpecificScopesArtifactFilter.java | 62 +--- .../ant/tasks/support/TypesArtifactFilter.java | 34 +- .../maven/ant/tasks/support/VersionMapper.java | 58 ++- .../apache/maven/plugins/antrun/AntRunMojo.java | 407 +++++++++------------ .../antrun/AntrunXmlPlexusConfigurationWriter.java | 98 ++--- .../maven/plugins/antrun/MavenAntRunProject.java | 15 +- .../apache/maven/plugins/antrun/MavenLogger.java | 25 +- .../AntrunXmlPlexusConfigurationWriterTest.java | 77 ++-- 11 files changed, 426 insertions(+), 607 deletions(-) diff --git a/pom.xml b/pom.xml index 2bd77f3..7ead9fc 100644 --- a/pom.xml +++ b/pom.xml @@ -1,5 +1,4 @@ -<?xml version='1.0' encoding='UTF-8'?> - +<?xml version="1.0" encoding="UTF-8"?> <!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file @@ -18,13 +17,12 @@ KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> - <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> - <artifactId>maven-plugins</artifactId> <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-plugins</artifactId> <version>39</version> <relativePath /> </parent> @@ -43,8 +41,8 @@ under the License. <scm> <connection>scm:git:https://gitbox.apache.org/repos/asf/maven-antrun-plugin.git</connection> <developerConnection>scm:git:https://gitbox.apache.org/repos/asf/maven-antrun-plugin.git</developerConnection> - <url>https://github.com/apache/maven-antrun-plugin/tree/${project.scm.tag}</url> <tag>HEAD</tag> + <url>https://github.com/apache/maven-antrun-plugin/tree/${project.scm.tag}</url> </scm> <issueManagement> <system>jira</system> @@ -157,28 +155,30 @@ under the License. <executions> <execution> <id>sources</id> - <phase>generate-sources</phase> <goals> <goal>java</goal> </goals> + <phase>generate-sources</phase> </execution> -<!-- <execution> --><!-- error: There are more than one class as root element for this version --> -<!-- <id>resources</id> --> -<!-- <phase>pre-site</phase> --> -<!-- <goals> --> -<!-- <goal>xsd</goal> --> -<!-- </goals> --> -<!-- <configuration> --> -<!-- <outputDirectory>target/generated-site/resources/xsd</outputDirectory> --> -<!-- </configuration> --> -<!-- </execution> --> -<!-- <execution> --><!-- error: There are more than one class as root element for this version --> -<!-- <id>documentation</id> --> -<!-- <phase>pre-site</phase> --> -<!-- <goals> --> -<!-- <goal>xdoc</goal> --> -<!-- </goals> --> -<!-- </execution> --> + <!-- <execution> --> + <!-- error: There are more than one class as root element for this version --> + <!-- <id>resources</id> --> + <!-- <phase>pre-site</phase> --> + <!-- <goals> --> + <!-- <goal>xsd</goal> --> + <!-- </goals> --> + <!-- <configuration> --> + <!-- <outputDirectory>target/generated-site/resources/xsd</outputDirectory> --> + <!-- </configuration> --> + <!-- </execution> --> + <!-- <execution> --> + <!-- error: There are more than one class as root element for this version --> + <!-- <id>documentation</id> --> + <!-- <phase>pre-site</phase> --> + <!-- <goals> --> + <!-- <goal>xdoc</goal> --> + <!-- </goals> --> + <!-- </execution> --> </executions> </plugin> </plugins> diff --git a/src/main/java/org/apache/maven/ant/tasks/AttachArtifactTask.java b/src/main/java/org/apache/maven/ant/tasks/AttachArtifactTask.java index 8e1f781..0212a4b 100644 --- a/src/main/java/org/apache/maven/ant/tasks/AttachArtifactTask.java +++ b/src/main/java/org/apache/maven/ant/tasks/AttachArtifactTask.java @@ -1,5 +1,3 @@ -package org.apache.maven.ant.tasks; - /* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file @@ -18,6 +16,9 @@ package org.apache.maven.ant.tasks; * specific language governing permissions and limitations * under the License. */ +package org.apache.maven.ant.tasks; + +import java.io.File; import org.apache.maven.plugins.antrun.AntRunMojo; import org.apache.maven.plugins.antrun.MavenAntRunProject; @@ -29,14 +30,10 @@ import org.apache.tools.ant.Project; import org.apache.tools.ant.Task; import org.codehaus.plexus.util.FileUtils; -import java.io.File; - /** - * + * */ -public class AttachArtifactTask - extends Task -{ +public class AttachArtifactTask extends Task { /** * The refId of the Maven project. @@ -46,95 +43,81 @@ public class AttachArtifactTask /** * The refId of the Maven project helper component. */ - @SuppressWarnings( "FieldCanBeLocal" ) + @SuppressWarnings("FieldCanBeLocal") private String mavenProjectHelperRefId = AntRunMojo.DEFAULT_MAVEN_PROJECT_HELPER_REFID; private AttachArtifactConfiguration configuration = new AttachArtifactConfiguration(); @Override - public void execute() - { + public void execute() { File file = configuration.getFile(); - if ( file == null ) - { - throw new BuildException( "File is a required parameter." ); + if (file == null) { + throw new BuildException("File is a required parameter."); } - if ( !file.exists() ) - { - throw new BuildException( "File does not exist: " + file ); + if (!file.exists()) { + throw new BuildException("File does not exist: " + file); } - if ( this.getProject().getReference( mavenProjectRefId ) == null ) - { - throw new BuildException( "Maven project reference not found: " + mavenProjectRefId ); + if (this.getProject().getReference(mavenProjectRefId) == null) { + throw new BuildException("Maven project reference not found: " + mavenProjectRefId); } String type = configuration.getType(); - if ( type == null ) - { - type = FileUtils.getExtension( file.getName() ); + if (type == null) { + type = FileUtils.getExtension(file.getName()); } MavenProject mavenProject = - ( (MavenAntRunProject) this.getProject().getReference( mavenProjectRefId ) ).getMavenProject(); + ((MavenAntRunProject) this.getProject().getReference(mavenProjectRefId)).getMavenProject(); - if ( this.getProject().getReference( mavenProjectHelperRefId ) == null ) - { - throw new BuildException( "Maven project helper reference not found: " + mavenProjectHelperRefId ); + if (this.getProject().getReference(mavenProjectHelperRefId) == null) { + throw new BuildException("Maven project helper reference not found: " + mavenProjectHelperRefId); } String classifier = configuration.getClassifier(); - log( "Attaching " + file + " as an attached artifact", Project.MSG_VERBOSE ); - MavenProjectHelper projectHelper = getProject().getReference( mavenProjectHelperRefId ); - projectHelper.attachArtifact( mavenProject, type, classifier, file ); + log("Attaching " + file + " as an attached artifact", Project.MSG_VERBOSE); + MavenProjectHelper projectHelper = getProject().getReference(mavenProjectHelperRefId); + projectHelper.attachArtifact(mavenProject, type, classifier, file); } /** * @return {@link #mavenProjectRefId} */ - public String getMavenProjectRefId() - { + public String getMavenProjectRefId() { return mavenProjectRefId; } /** * @param mavenProjectRefId {@link #mavenProjectRefId} */ - public void setMavenProjectRefId( String mavenProjectRefId ) - { + public void setMavenProjectRefId(String mavenProjectRefId) { this.mavenProjectRefId = mavenProjectRefId; } /* Fields delegated to AttachArtifactConfiguration */ - - public File getFile() - { + + public File getFile() { return this.configuration.getFile(); } - public void setFile( File file ) - { - this.configuration.setFile( file ); + public void setFile(File file) { + this.configuration.setFile(file); } - public String getClassifier() - { + public String getClassifier() { return this.configuration.getClassifier(); } - public void setClassifier( String classifier ) - { - this.configuration.setClassifier( classifier ); + public void setClassifier(String classifier) { + this.configuration.setClassifier(classifier); } - public String getType() - { + public String getType() { return this.configuration.getType(); } - public void setType( String type ) - { - this.configuration.setType( type ); + public void setType(String type) { + this.configuration.setType(type); } } diff --git a/src/main/java/org/apache/maven/ant/tasks/DependencyFilesetsTask.java b/src/main/java/org/apache/maven/ant/tasks/DependencyFilesetsTask.java index d307b7b..818f506 100644 --- a/src/main/java/org/apache/maven/ant/tasks/DependencyFilesetsTask.java +++ b/src/main/java/org/apache/maven/ant/tasks/DependencyFilesetsTask.java @@ -1,5 +1,3 @@ -package org.apache.maven.ant.tasks; - /* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file @@ -18,6 +16,11 @@ package org.apache.maven.ant.tasks; * specific language governing permissions and limitations * under the License. */ +package org.apache.maven.ant.tasks; + +import java.io.File; +import java.util.LinkedHashSet; +import java.util.Set; import org.apache.maven.ant.tasks.support.SpecificScopesArtifactFilter; import org.apache.maven.ant.tasks.support.TypesArtifactFilter; @@ -31,19 +34,13 @@ import org.apache.tools.ant.BuildException; import org.apache.tools.ant.Task; import org.apache.tools.ant.types.FileSet; -import java.io.File; -import java.util.LinkedHashSet; -import java.util.Set; - /** * Ant task which create a fileset for each dependency in a Maven project, and a * fileset containing all selected dependencies. * * @author pgier */ -public class DependencyFilesetsTask - extends Task -{ +public class DependencyFilesetsTask extends Task { /** * The project ref Id of the project being used. */ @@ -53,71 +50,63 @@ public class DependencyFilesetsTask /** {@inheritDoc} */ @Override - public void execute() - { - if ( this.getProject().getReference( mavenProjectId ) == null ) - { - throw new BuildException( "Maven project reference not found: " + mavenProjectId ); + public void execute() { + if (this.getProject().getReference(mavenProjectId) == null) { + throw new BuildException("Maven project reference not found: " + mavenProjectId); } - MavenProject mavenProject = this.getProject().getReference( "maven.project" ); + MavenProject mavenProject = this.getProject().getReference("maven.project"); // Add filesets for depenedency artifacts - Set<Artifact> depArtifacts = filterArtifacts( mavenProject.getArtifacts() ); + Set<Artifact> depArtifacts = filterArtifacts(mavenProject.getArtifacts()); FileSet dependenciesFileSet = new FileSet(); - dependenciesFileSet.setProject( getProject() ); - ArtifactRepository localRepository = getProject().getReference( "maven.local.repository" ); - dependenciesFileSet.setDir( new File( localRepository.getBasedir() ) ); + dependenciesFileSet.setProject(getProject()); + ArtifactRepository localRepository = getProject().getReference("maven.local.repository"); + dependenciesFileSet.setDir(new File(localRepository.getBasedir())); - if ( depArtifacts.isEmpty() ) - { + if (depArtifacts.isEmpty()) { // For performance reasons in case of huge local repo, tell Ant to include a single thing, otherwise the // whole directory is scanned (even though ** is excluded). - dependenciesFileSet.createInclude().setName( "." ); - dependenciesFileSet.createExclude().setName( "**" ); + dependenciesFileSet.createInclude().setName("."); + dependenciesFileSet.createExclude().setName("**"); } - for ( Artifact artifact : depArtifacts ) - { - String relativeArtifactPath = localRepository.pathOf( artifact ); - dependenciesFileSet.createInclude().setName( relativeArtifactPath ); + for (Artifact artifact : depArtifacts) { + String relativeArtifactPath = localRepository.pathOf(artifact); + dependenciesFileSet.createInclude().setName(relativeArtifactPath); String fileSetName = getPrefix() + artifact.getDependencyConflictId(); FileSet singleArtifactFileSet = new FileSet(); - singleArtifactFileSet.setProject( getProject() ); - singleArtifactFileSet.setFile( artifact.getFile() ); - getProject().addReference( fileSetName, singleArtifactFileSet ); + singleArtifactFileSet.setProject(getProject()); + singleArtifactFileSet.setFile(artifact.getFile()); + getProject().addReference(fileSetName, singleArtifactFileSet); } - getProject().addReference( ( getPrefix() + getProjectDependenciesId() ), dependenciesFileSet ); + getProject().addReference((getPrefix() + getProjectDependenciesId()), dependenciesFileSet); } /** * @return {@link #mavenProjectId} */ - public String getMavenProjectId() - { + public String getMavenProjectId() { return mavenProjectId; } /** * @param mavenProjectId {@link #mavenProjectId} */ - public void setMavenProjectId( String mavenProjectId ) - { + public void setMavenProjectId(String mavenProjectId) { this.mavenProjectId = mavenProjectId; } /** * @return prefix Prefix to be added to each of the dependency filesets */ - public String getPrefix() - { + public String getPrefix() { String prefix = configuration.getPrefix(); - if ( prefix == null ) - { + if (prefix == null) { prefix = ""; } return prefix; @@ -127,57 +116,50 @@ public class DependencyFilesetsTask * Prefix to be added to each of the dependency filesets. Default is empty string. * @param prefix String to prepend to all fileset IDs. */ - public void setPrefix( String prefix ) - { - this.configuration.setPrefix( prefix ); + public void setPrefix(String prefix) { + this.configuration.setPrefix(prefix); } /** * @return types Comma separated list of artifact types to include. */ - public String getTypes() - { + public String getTypes() { return this.configuration.getTypes(); } /** * @param types Comma separated list of artifact types to include. */ - public void setTypes( String types ) - { - this.configuration.setTypes( types ); + public void setTypes(String types) { + this.configuration.setTypes(types); } /** * @return scopes Comma separated list of artifact scopes to include. */ - public String getScopes() - { + public String getScopes() { return this.configuration.getScopes(); } /** * @param scopes Comma separated list of artifact scopes to include. */ - public void setScopes( String scopes ) - { - this.configuration.setScopes( scopes ); + public void setScopes(String scopes) { + this.configuration.setScopes(scopes); } /** * @return RefId for the fileset containing all project dependencies - default maven.project.dependencies */ - public String getProjectDependenciesId() - { + public String getProjectDependenciesId() { return this.configuration.getProjectDependenciesId(); } /** * @param projectDependenciesId RefId for the fileset containing all project dependencies */ - public void setProjectDependenciesId( String projectDependenciesId ) - { - this.configuration.setProjectDependenciesId( projectDependenciesId ); + public void setProjectDependenciesId(String projectDependenciesId) { + this.configuration.setProjectDependenciesId(projectDependenciesId); } /** @@ -186,41 +168,33 @@ public class DependencyFilesetsTask * @param artifacts {@link Artifact} set. * @return The set of filtered artifacts. */ - public Set<Artifact> filterArtifacts( Set<Artifact> artifacts ) - { + public Set<Artifact> filterArtifacts(Set<Artifact> artifacts) { String scopes = getScopes(); - if ( scopes == null ) - { + if (scopes == null) { scopes = ""; } - + String types = getTypes(); - if ( types == null ) - { + if (types == null) { types = ""; } - if ( "".equals( scopes ) && "".equals( types ) ) - { + if ("".equals(scopes) && "".equals(types)) { return artifacts; } AndArtifactFilter filter = new AndArtifactFilter(); - if ( !"".equals( scopes ) ) - { - filter.add( new SpecificScopesArtifactFilter( getScopes() ) ); + if (!"".equals(scopes)) { + filter.add(new SpecificScopesArtifactFilter(getScopes())); } - if ( !"".equals( types ) ) - { - filter.add( new TypesArtifactFilter( getTypes() ) ); + if (!"".equals(types)) { + filter.add(new TypesArtifactFilter(getTypes())); } Set<Artifact> artifactsResult = new LinkedHashSet<>(); - for ( Artifact artifact : artifacts ) - { - if ( filter.include( artifact ) ) - { - artifactsResult.add( artifact ); + for (Artifact artifact : artifacts) { + if (filter.include(artifact)) { + artifactsResult.add(artifact); } } return artifactsResult; diff --git a/src/main/java/org/apache/maven/ant/tasks/support/SpecificScopesArtifactFilter.java b/src/main/java/org/apache/maven/ant/tasks/support/SpecificScopesArtifactFilter.java index c5355f6..1e3ed12 100644 --- a/src/main/java/org/apache/maven/ant/tasks/support/SpecificScopesArtifactFilter.java +++ b/src/main/java/org/apache/maven/ant/tasks/support/SpecificScopesArtifactFilter.java @@ -1,5 +1,3 @@ -package org.apache.maven.ant.tasks.support; - /* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file @@ -9,7 +7,7 @@ package org.apache.maven.ant.tasks.support; * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://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 @@ -18,6 +16,7 @@ package org.apache.maven.ant.tasks.support; * specific language governing permissions and limitations * under the License. */ +package org.apache.maven.ant.tasks.support; import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.resolver.filter.ArtifactFilter; @@ -27,9 +26,7 @@ import org.apache.maven.artifact.resolver.filter.ArtifactFilter; * * @author pgier */ -public class SpecificScopesArtifactFilter - implements ArtifactFilter -{ +public class SpecificScopesArtifactFilter implements ArtifactFilter { private boolean compileScope; private boolean runtimeScope; @@ -45,30 +42,19 @@ public class SpecificScopesArtifactFilter * * @param scopes A comma separated list of scopes */ - public SpecificScopesArtifactFilter( String scopes ) - { - String[] scopeList = scopes.split( "," ); + public SpecificScopesArtifactFilter(String scopes) { + String[] scopeList = scopes.split(","); - for ( String aScopeList : scopeList ) - { - if ( aScopeList.trim().equals( Artifact.SCOPE_COMPILE ) ) - { + for (String aScopeList : scopeList) { + if (aScopeList.trim().equals(Artifact.SCOPE_COMPILE)) { compileScope = true; - } - else if ( aScopeList.trim().equals( Artifact.SCOPE_PROVIDED ) ) - { + } else if (aScopeList.trim().equals(Artifact.SCOPE_PROVIDED)) { providedScope = true; - } - else if ( aScopeList.trim().equals( Artifact.SCOPE_RUNTIME ) ) - { + } else if (aScopeList.trim().equals(Artifact.SCOPE_RUNTIME)) { runtimeScope = true; - } - else if ( aScopeList.trim().equals( Artifact.SCOPE_SYSTEM ) ) - { + } else if (aScopeList.trim().equals(Artifact.SCOPE_SYSTEM)) { systemScope = true; - } - else if ( aScopeList.trim().equals( Artifact.SCOPE_TEST ) ) - { + } else if (aScopeList.trim().equals(Artifact.SCOPE_TEST)) { testScope = true; } } @@ -76,30 +62,18 @@ public class SpecificScopesArtifactFilter /** {@inheritDoc} */ @Override - public boolean include( Artifact artifact ) - { - if ( Artifact.SCOPE_COMPILE.equals( artifact.getScope() ) ) - { + public boolean include(Artifact artifact) { + if (Artifact.SCOPE_COMPILE.equals(artifact.getScope())) { return compileScope; - } - else if ( Artifact.SCOPE_RUNTIME.equals( artifact.getScope() ) ) - { + } else if (Artifact.SCOPE_RUNTIME.equals(artifact.getScope())) { return runtimeScope; - } - else if ( Artifact.SCOPE_TEST.equals( artifact.getScope() ) ) - { + } else if (Artifact.SCOPE_TEST.equals(artifact.getScope())) { return testScope; - } - else if ( Artifact.SCOPE_PROVIDED.equals( artifact.getScope() ) ) - { + } else if (Artifact.SCOPE_PROVIDED.equals(artifact.getScope())) { return providedScope; - } - else if ( Artifact.SCOPE_SYSTEM.equals( artifact.getScope() ) ) - { + } else if (Artifact.SCOPE_SYSTEM.equals(artifact.getScope())) { return systemScope; - } - else - { + } else { return true; } } diff --git a/src/main/java/org/apache/maven/ant/tasks/support/TypesArtifactFilter.java b/src/main/java/org/apache/maven/ant/tasks/support/TypesArtifactFilter.java index bd12865..2365463 100644 --- a/src/main/java/org/apache/maven/ant/tasks/support/TypesArtifactFilter.java +++ b/src/main/java/org/apache/maven/ant/tasks/support/TypesArtifactFilter.java @@ -1,5 +1,3 @@ -package org.apache.maven.ant.tasks.support; - /* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file @@ -9,7 +7,7 @@ package org.apache.maven.ant.tasks.support; * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://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 @@ -18,19 +16,18 @@ package org.apache.maven.ant.tasks.support; * specific language governing permissions and limitations * under the License. */ - -import org.apache.maven.artifact.Artifact; -import org.apache.maven.artifact.resolver.filter.ArtifactFilter; +package org.apache.maven.ant.tasks.support; import java.util.ArrayList; import java.util.List; +import org.apache.maven.artifact.Artifact; +import org.apache.maven.artifact.resolver.filter.ArtifactFilter; + /** * Artifact Filter which filters on artifact types. */ -public class TypesArtifactFilter - implements ArtifactFilter -{ +public class TypesArtifactFilter implements ArtifactFilter { private List<String> types = new ArrayList<>(); /** @@ -38,26 +35,21 @@ public class TypesArtifactFilter * * @param types The types. */ - public TypesArtifactFilter( String types ) - { - if ( !types.trim().equals( "" ) ) - { - for ( String type : types.split( "," ) ) - { - this.types.add( type.trim() ); + public TypesArtifactFilter(String types) { + if (!types.trim().equals("")) { + for (String type : types.split(",")) { + this.types.add(type.trim()); } } } /** {@inheritDoc} */ @Override - public boolean include( Artifact artifact ) - { + public boolean include(Artifact artifact) { String artifactType = artifact.getType(); - if ( artifactType == null || artifactType.equals( "" ) ) - { + if (artifactType == null || artifactType.equals("")) { artifactType = "jar"; } - return types.contains( artifactType ); + return types.contains(artifactType); } } diff --git a/src/main/java/org/apache/maven/ant/tasks/support/VersionMapper.java b/src/main/java/org/apache/maven/ant/tasks/support/VersionMapper.java index 4f5f980..fac67ae 100644 --- a/src/main/java/org/apache/maven/ant/tasks/support/VersionMapper.java +++ b/src/main/java/org/apache/maven/ant/tasks/support/VersionMapper.java @@ -1,5 +1,3 @@ -package org.apache.maven.ant.tasks.support; - /* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file @@ -9,7 +7,7 @@ package org.apache.maven.ant.tasks.support; * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://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 @@ -18,49 +16,44 @@ package org.apache.maven.ant.tasks.support; * specific language governing permissions and limitations * under the License. */ - -import org.apache.tools.ant.util.FileNameMapper; -import org.codehaus.plexus.util.StringUtils; +package org.apache.maven.ant.tasks.support; import java.io.File; import java.util.Arrays; import java.util.Comparator; import java.util.List; +import org.apache.tools.ant.util.FileNameMapper; +import org.codehaus.plexus.util.StringUtils; + /** * Ant filename mapper to remove version info from filename when copying dependencies. * * @author <a href="mailto:hbout...@apache.org">Herve Boutemy</a> */ -public class VersionMapper - implements FileNameMapper, Comparator<String> -{ +public class VersionMapper implements FileNameMapper, Comparator<String> { private List<String> versions; private String to; /** {@inheritDoc} */ @Override - public String[] mapFileName( String sourceFileName ) - { - String originalFileName = new File( sourceFileName ).getName(); - for ( String version : versions ) - { - int index = originalFileName.indexOf( version ); - if ( index >= 0 ) - { + public String[] mapFileName(String sourceFileName) { + String originalFileName = new File(sourceFileName).getName(); + for (String version : versions) { + int index = originalFileName.indexOf(version); + if (index >= 0) { // remove version in artifactId-version(-classifier).type - String baseFilename = originalFileName.substring( 0, index - 1 ); - String extension = originalFileName.substring( index + version.length() ); - String path = sourceFileName.substring( 0, sourceFileName.length() - originalFileName.length() ); - if ( "flatten".equals( to ) ) - { + String baseFilename = originalFileName.substring(0, index - 1); + String extension = originalFileName.substring(index + version.length()); + String path = sourceFileName.substring(0, sourceFileName.length() - originalFileName.length()); + if ("flatten".equals(to)) { path = ""; } - return new String[]{ path + baseFilename + extension }; + return new String[] {path + baseFilename + extension}; } } - return new String[]{ sourceFileName }; + return new String[] {sourceFileName}; } /** @@ -69,12 +62,11 @@ public class VersionMapper * @param from The string from which we set. */ @Override - public void setFrom( String from ) - { - String[] split = StringUtils.split( from, File.pathSeparator ); + public void setFrom(String from) { + String[] split = StringUtils.split(from, File.pathSeparator); // sort, from lengthiest to smallest - Arrays.sort( split, this ); - versions = Arrays.asList( split ); + Arrays.sort(split, this); + versions = Arrays.asList(split); } /** @@ -82,16 +74,14 @@ public class VersionMapper * @param to {@link #to} */ @Override - public void setTo( String to ) - { + public void setTo(String to) { this.to = to; } /** {@inheritDoc} */ @Override - public int compare( String s1, String s2 ) - { + public int compare(String s1, String s2) { int lengthDiff = s2.length() - s1.length(); - return ( lengthDiff != 0 ) ? lengthDiff : s1.compareTo( s2 ); + return (lengthDiff != 0) ? lengthDiff : s1.compareTo(s2); } } diff --git a/src/main/java/org/apache/maven/plugins/antrun/AntRunMojo.java b/src/main/java/org/apache/maven/plugins/antrun/AntRunMojo.java index a0f0892..955645d 100644 --- a/src/main/java/org/apache/maven/plugins/antrun/AntRunMojo.java +++ b/src/main/java/org/apache/maven/plugins/antrun/AntRunMojo.java @@ -1,5 +1,3 @@ -package org.apache.maven.plugins.antrun; - /* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file @@ -18,6 +16,7 @@ package org.apache.maven.plugins.antrun; * specific language governing permissions and limitations * under the License. */ +package org.apache.maven.plugins.antrun; import java.io.File; import java.io.IOException; @@ -64,10 +63,8 @@ import org.codehaus.plexus.util.StringUtils; * @author <a href="mailto:ken...@apache.org">Kenney Westerhof</a> * @author <a href="mailto:vincent.sive...@gmail.com">Vincent Siveton</a> */ -@Mojo( name = "run", threadSafe = true, requiresDependencyResolution = ResolutionScope.TEST ) -public class AntRunMojo - extends AbstractMojo -{ +@Mojo(name = "run", threadSafe = true, requiresDependencyResolution = ResolutionScope.TEST) +public class AntRunMojo extends AbstractMojo { /** * The prefix of all refid used by the plugin. @@ -117,13 +114,13 @@ public class AntRunMojo /** * The Maven project object */ - @Parameter( defaultValue = "${project}", readonly = true, required = true ) + @Parameter(defaultValue = "${project}", readonly = true, required = true) private MavenProject mavenProject; /** * The Maven session object */ - @Parameter( defaultValue = "${session}", readonly = true, required = true ) + @Parameter(defaultValue = "${session}", readonly = true, required = true) private MavenSession session; /** @@ -135,13 +132,13 @@ public class AntRunMojo /** * The plugin dependencies. */ - @Parameter( property = "plugin.artifacts", required = true, readonly = true ) + @Parameter(property = "plugin.artifacts", required = true, readonly = true) private List<Artifact> pluginArtifacts; /** * The local Maven repository */ - @Parameter( property = "localRepository", readonly = true ) + @Parameter(property = "localRepository", readonly = true) protected ArtifactRepository localRepository; /** @@ -149,13 +146,13 @@ public class AntRunMojo * * @since 1.4 */ - @Parameter( defaultValue = "" ) + @Parameter(defaultValue = "") private String propertyPrefix; /** * Maven will look in the target-tag for the namespace of <code>http://maven.apache.org/ANTRUN</code> * or <code>antlib:org.apache.maven.ant.tasks</code> - * + * * <pre> * <configuration> * <target xmlns:mvn="http://maven.apache.org/ANTRUN"> @@ -164,8 +161,8 @@ public class AntRunMojo * </target> * </configuration> * </pre> - * - * @deprecated only here for backwards compatibility + * + * @deprecated only here for backwards compatibility * @since 1.5 */ @Deprecated @@ -176,7 +173,7 @@ public class AntRunMojo * The name of a property containing the list of all dependency versions. This is used for the removing the versions * from the filenames. */ - @Parameter( defaultValue = "maven.project.dependencies.versions" ) + @Parameter(defaultValue = "maven.project.dependencies.versions") private String versionsPropertyName; /** @@ -206,9 +203,9 @@ public class AntRunMojo * @deprecated Use the <code>build-helper-maven-plugin</code> to bind source directories. For version 3.0.0, this * parameter is only defined to break the build if you use it! */ - @SuppressWarnings( "DeprecatedIsStillUsed" ) + @SuppressWarnings("DeprecatedIsStillUsed") @Deprecated - @Parameter( property = "sourceRoot" ) + @Parameter(property = "sourceRoot") private File sourceRoot; /** @@ -218,9 +215,9 @@ public class AntRunMojo * @deprecated Use the <code>build-helper-maven-plugin</code> to bind test source directories. For version 3.0.0, * this parameter is only defined to break the build if you use it! */ - @SuppressWarnings( "DeprecatedIsStillUsed" ) + @SuppressWarnings("DeprecatedIsStillUsed") @Deprecated - @Parameter( property = "testSourceRoot" ) + @Parameter(property = "testSourceRoot") private File testSourceRoot; /** @@ -228,7 +225,7 @@ public class AntRunMojo * * @since 1.7 */ - @Parameter( property = "maven.antrun.skip", defaultValue = "false" ) + @Parameter(property = "maven.antrun.skip", defaultValue = "false") private boolean skip; /** @@ -236,7 +233,7 @@ public class AntRunMojo * * @since 1.7 */ - @Parameter( defaultValue = "false" ) + @Parameter(defaultValue = "false") private boolean exportAntProperties; /** @@ -246,147 +243,120 @@ public class AntRunMojo * * @since 1.7 */ - @Parameter( defaultValue = "true" ) + @Parameter(defaultValue = "true") private boolean failOnError; @Override - public void execute() - throws MojoExecutionException, MojoFailureException - { - checkDeprecatedParameterUsage( tasks, "tasks", "target" ); - checkDeprecatedParameterUsage( sourceRoot, "sourceRoot", "the build-helper-maven-plugin" ); - checkDeprecatedParameterUsage( testSourceRoot, "testSourceRoot", "the build-helper-maven-plugin" ); - if ( skip ) - { - getLog().info( "Skipping Antrun execution" ); + public void execute() throws MojoExecutionException, MojoFailureException { + checkDeprecatedParameterUsage(tasks, "tasks", "target"); + checkDeprecatedParameterUsage(sourceRoot, "sourceRoot", "the build-helper-maven-plugin"); + checkDeprecatedParameterUsage(testSourceRoot, "testSourceRoot", "the build-helper-maven-plugin"); + if (skip) { + getLog().info("Skipping Antrun execution"); return; } - if ( target == null ) - { - getLog().info( "No Ant target defined - SKIPPED" ); + if (target == null) { + getLog().info("No Ant target defined - SKIPPED"); return; } - if ( propertyPrefix == null ) - { + if (propertyPrefix == null) { propertyPrefix = ""; } - String antTargetName = target.getAttribute( "name", DEFAULT_ANT_TARGET_NAME ); - target.setAttribute( "name", antTargetName ); + String antTargetName = target.getAttribute("name", DEFAULT_ANT_TARGET_NAME); + target.setAttribute("name", antTargetName); Project antProject = new Project(); - antProject.addBuildListener( getConfiguredBuildLogger() ); - try - { - File antBuildFile = writeTargetToProjectFile( antTargetName ); - ProjectHelper.configureProject( antProject, antBuildFile ); + antProject.addBuildListener(getConfiguredBuildLogger()); + try { + File antBuildFile = writeTargetToProjectFile(antTargetName); + ProjectHelper.configureProject(antProject, antBuildFile); antProject.init(); - antProject.setBaseDir( mavenProject.getBasedir() ); + antProject.setBaseDir(mavenProject.getBasedir()); - addAntProjectReferences( mavenProject, antProject ); - initMavenTasks( antProject ); + addAntProjectReferences(mavenProject, antProject); + initMavenTasks(antProject); // The Ant project needs actual properties vs. using expression evaluator when calling an external build // file. - copyProperties( mavenProject, antProject ); + copyProperties(mavenProject, antProject); - getLog().info( "Executing tasks" ); - antProject.executeTarget( antTargetName ); - getLog().info( "Executed tasks" ); + getLog().info("Executing tasks"); + antProject.executeTarget(antTargetName); + getLog().info("Executed tasks"); - copyProperties( antProject, mavenProject ); - } - catch ( BuildException e ) - { + copyProperties(antProject, mavenProject); + } catch (BuildException e) { StringBuilder sb = new StringBuilder(); - sb.append( "An Ant BuildException has occured: " ).append( e.getMessage() ); - String fragment = findFragment( e ); - if ( fragment != null ) - { - sb.append( "\n" ).append( fragment ); + sb.append("An Ant BuildException has occured: ").append(e.getMessage()); + String fragment = findFragment(e); + if (fragment != null) { + sb.append("\n").append(fragment); } - if ( !failOnError ) - { - getLog().info( sb.toString(), e ); + if (!failOnError) { + getLog().info(sb.toString(), e); return; // do not register roots. + } else { + throw new MojoExecutionException(sb.toString(), e); } - else - { - throw new MojoExecutionException( sb.toString(), e ); - } - } - catch ( Throwable e ) - { - throw new MojoExecutionException( "Error executing Ant tasks: " + e.getMessage(), e ); + } catch (Throwable e) { + throw new MojoExecutionException("Error executing Ant tasks: " + e.getMessage(), e); } } - private void checkDeprecatedParameterUsage( Object parameter, String name, String replacement ) - throws MojoFailureException - { - if ( parameter != null ) - { - throw new MojoFailureException( "You are using '" + name + "' which has been removed" - + " from the maven-antrun-plugin. Please use '" + replacement - + "' and refer to the >>Major Version Upgrade to version 3.0.0<< " + "on the plugin site." ); + private void checkDeprecatedParameterUsage(Object parameter, String name, String replacement) + throws MojoFailureException { + if (parameter != null) { + throw new MojoFailureException("You are using '" + name + "' which has been removed" + + " from the maven-antrun-plugin. Please use '" + replacement + + "' and refer to the >>Major Version Upgrade to version 3.0.0<< " + "on the plugin site."); } } - private DefaultLogger getConfiguredBuildLogger() - { - DefaultLogger antLogger = new MavenLogger( getLog() ); - if ( getLog().isDebugEnabled() ) - { - antLogger.setMessageOutputLevel( Project.MSG_DEBUG ); - } - else if ( getLog().isInfoEnabled() ) - { - antLogger.setMessageOutputLevel( Project.MSG_INFO ); - } - else if ( getLog().isWarnEnabled() ) - { - antLogger.setMessageOutputLevel( Project.MSG_WARN ); - } - else if ( getLog().isErrorEnabled() ) - { - antLogger.setMessageOutputLevel( Project.MSG_ERR ); - } - else - { - antLogger.setMessageOutputLevel( Project.MSG_VERBOSE ); + private DefaultLogger getConfiguredBuildLogger() { + DefaultLogger antLogger = new MavenLogger(getLog()); + if (getLog().isDebugEnabled()) { + antLogger.setMessageOutputLevel(Project.MSG_DEBUG); + } else if (getLog().isInfoEnabled()) { + antLogger.setMessageOutputLevel(Project.MSG_INFO); + } else if (getLog().isWarnEnabled()) { + antLogger.setMessageOutputLevel(Project.MSG_WARN); + } else if (getLog().isErrorEnabled()) { + antLogger.setMessageOutputLevel(Project.MSG_ERR); + } else { + antLogger.setMessageOutputLevel(Project.MSG_VERBOSE); } return antLogger; } - private void addAntProjectReferences( MavenProject mavenProject, Project antProject ) - throws DependencyResolutionRequiredException - { - Path p = new Path( antProject ); - p.setPath( StringUtils.join( mavenProject.getCompileClasspathElements().iterator(), File.pathSeparator ) ); + private void addAntProjectReferences(MavenProject mavenProject, Project antProject) + throws DependencyResolutionRequiredException { + Path p = new Path(antProject); + p.setPath(StringUtils.join(mavenProject.getCompileClasspathElements().iterator(), File.pathSeparator)); /* maven.dependency.classpath it's deprecated as it's equal to maven.compile.classpath */ - antProject.addReference( MAVEN_REFID_PREFIX + "dependency.classpath", p ); - antProject.addReference( MAVEN_REFID_PREFIX + "compile.classpath", p ); + antProject.addReference(MAVEN_REFID_PREFIX + "dependency.classpath", p); + antProject.addReference(MAVEN_REFID_PREFIX + "compile.classpath", p); - p = new Path( antProject ); - p.setPath( StringUtils.join( mavenProject.getRuntimeClasspathElements().iterator(), File.pathSeparator ) ); - antProject.addReference( MAVEN_REFID_PREFIX + "runtime.classpath", p ); + p = new Path(antProject); + p.setPath(StringUtils.join(mavenProject.getRuntimeClasspathElements().iterator(), File.pathSeparator)); + antProject.addReference(MAVEN_REFID_PREFIX + "runtime.classpath", p); - p = new Path( antProject ); - p.setPath( StringUtils.join( mavenProject.getTestClasspathElements().iterator(), File.pathSeparator ) ); - antProject.addReference( MAVEN_REFID_PREFIX + "test.classpath", p ); + p = new Path(antProject); + p.setPath(StringUtils.join(mavenProject.getTestClasspathElements().iterator(), File.pathSeparator)); + antProject.addReference(MAVEN_REFID_PREFIX + "test.classpath", p); /* set maven.plugin.classpath with plugin dependencies */ - antProject.addReference( MAVEN_REFID_PREFIX + "plugin.classpath", - getPathFromArtifacts( pluginArtifacts, antProject ) ); + antProject.addReference( + MAVEN_REFID_PREFIX + "plugin.classpath", getPathFromArtifacts(pluginArtifacts, antProject)); - antProject.addReference( DEFAULT_MAVEN_PROJECT_REFID, mavenProject ); - antProject.addReference( DEFAULT_MAVEN_PROJECT_REF_REFID, new MavenAntRunProject( mavenProject ) ); - antProject.addReference( DEFAULT_MAVEN_PROJECT_HELPER_REFID, projectHelper ); - antProject.addReference( MAVEN_REFID_PREFIX + "local.repository", localRepository ); + antProject.addReference(DEFAULT_MAVEN_PROJECT_REFID, mavenProject); + antProject.addReference(DEFAULT_MAVEN_PROJECT_REF_REFID, new MavenAntRunProject(mavenProject)); + antProject.addReference(DEFAULT_MAVEN_PROJECT_HELPER_REFID, projectHelper); + antProject.addReference(MAVEN_REFID_PREFIX + "local.repository", localRepository); } /** @@ -395,27 +365,23 @@ public class AntRunMojo * @return {@link Path} * @throws DependencyResolutionRequiredException In case of a failure. */ - private Path getPathFromArtifacts( Collection<Artifact> artifacts, Project antProject ) - throws DependencyResolutionRequiredException - { - if ( artifacts == null ) - { - return new Path( antProject ); + private Path getPathFromArtifacts(Collection<Artifact> artifacts, Project antProject) + throws DependencyResolutionRequiredException { + if (artifacts == null) { + return new Path(antProject); } - List<String> list = new ArrayList<>( artifacts.size() ); - for ( Artifact a : artifacts ) - { + List<String> list = new ArrayList<>(artifacts.size()); + for (Artifact a : artifacts) { File file = a.getFile(); - if ( file == null ) - { - throw new DependencyResolutionRequiredException( a ); + if (file == null) { + throw new DependencyResolutionRequiredException(a); } - list.add( file.getPath() ); + list.add(file.getPath()); } - Path p = new Path( antProject ); - p.setPath( StringUtils.join( list.iterator(), File.pathSeparator ) ); + Path p = new Path(antProject); + p.setPath(StringUtils.join(list.iterator(), File.pathSeparator)); return p; } @@ -426,61 +392,61 @@ public class AntRunMojo * @param mavenProject {@link MavenProject} * @param antProject {@link Project} */ - public void copyProperties( MavenProject mavenProject, Project antProject ) - { + public void copyProperties(MavenProject mavenProject, Project antProject) { Properties mavenProps = mavenProject.getProperties(); Properties userProps = session.getUserProperties(); - List<String> allPropertyKeys = new ArrayList<>( mavenProps.stringPropertyNames() ); - allPropertyKeys.addAll( userProps.stringPropertyNames() ); - for ( String key : allPropertyKeys ) - { - String value = userProps.getProperty( key, mavenProps.getProperty( key ) ); - antProject.setProperty( key, value ); + List<String> allPropertyKeys = new ArrayList<>(mavenProps.stringPropertyNames()); + allPropertyKeys.addAll(userProps.stringPropertyNames()); + for (String key : allPropertyKeys) { + String value = userProps.getProperty(key, mavenProps.getProperty(key)); + antProject.setProperty(key, value); } // Set the POM file as the ant.file for the tasks run directly in Maven. - antProject.setProperty( "ant.file", mavenProject.getFile().getAbsolutePath() ); + antProject.setProperty("ant.file", mavenProject.getFile().getAbsolutePath()); // Add some of the common Maven properties - getLog().debug( "Setting properties with prefix: " + propertyPrefix ); - antProject.setProperty( ( propertyPrefix + "project.groupId" ), mavenProject.getGroupId() ); - antProject.setProperty( ( propertyPrefix + "project.artifactId" ), mavenProject.getArtifactId() ); - antProject.setProperty( ( propertyPrefix + "project.name" ), mavenProject.getName() ); - if ( mavenProject.getDescription() != null ) - { - antProject.setProperty( ( propertyPrefix + "project.description" ), mavenProject.getDescription() ); + getLog().debug("Setting properties with prefix: " + propertyPrefix); + antProject.setProperty((propertyPrefix + "project.groupId"), mavenProject.getGroupId()); + antProject.setProperty((propertyPrefix + "project.artifactId"), mavenProject.getArtifactId()); + antProject.setProperty((propertyPrefix + "project.name"), mavenProject.getName()); + if (mavenProject.getDescription() != null) { + antProject.setProperty((propertyPrefix + "project.description"), mavenProject.getDescription()); } - antProject.setProperty( ( propertyPrefix + "project.version" ), mavenProject.getVersion() ); - antProject.setProperty( ( propertyPrefix + "project.packaging" ), mavenProject.getPackaging() ); - antProject.setProperty( ( propertyPrefix + "project.build.directory" ), - mavenProject.getBuild().getDirectory() ); - antProject.setProperty( ( propertyPrefix + "project.build.outputDirectory" ), - mavenProject.getBuild().getOutputDirectory() ); - antProject.setProperty( ( propertyPrefix + "project.build.testOutputDirectory" ), - mavenProject.getBuild().getTestOutputDirectory() ); - antProject.setProperty( ( propertyPrefix + "project.build.sourceDirectory" ), - mavenProject.getBuild().getSourceDirectory() ); - antProject.setProperty( ( propertyPrefix + "project.build.testSourceDirectory" ), - mavenProject.getBuild().getTestSourceDirectory() ); - antProject.setProperty( ( propertyPrefix + "localRepository" ), localRepository.toString() ); - antProject.setProperty( ( propertyPrefix + "settings.localRepository" ), localRepository.getBasedir() ); + antProject.setProperty((propertyPrefix + "project.version"), mavenProject.getVersion()); + antProject.setProperty((propertyPrefix + "project.packaging"), mavenProject.getPackaging()); + antProject.setProperty( + (propertyPrefix + "project.build.directory"), + mavenProject.getBuild().getDirectory()); + antProject.setProperty( + (propertyPrefix + "project.build.outputDirectory"), + mavenProject.getBuild().getOutputDirectory()); + antProject.setProperty( + (propertyPrefix + "project.build.testOutputDirectory"), + mavenProject.getBuild().getTestOutputDirectory()); + antProject.setProperty( + (propertyPrefix + "project.build.sourceDirectory"), + mavenProject.getBuild().getSourceDirectory()); + antProject.setProperty( + (propertyPrefix + "project.build.testSourceDirectory"), + mavenProject.getBuild().getTestSourceDirectory()); + antProject.setProperty((propertyPrefix + "localRepository"), localRepository.toString()); + antProject.setProperty((propertyPrefix + "settings.localRepository"), localRepository.getBasedir()); // Add properties for dependency artifacts Set<Artifact> depArtifacts = mavenProject.getArtifacts(); - for ( Artifact artifact : depArtifacts ) - { + for (Artifact artifact : depArtifacts) { String propName = artifact.getDependencyConflictId(); - antProject.setProperty( propertyPrefix + propName, artifact.getFile().getPath() ); + antProject.setProperty(propertyPrefix + propName, artifact.getFile().getPath()); } // Add a property containing the list of versions for the mapper StringBuilder versionsBuffer = new StringBuilder(); - for ( Artifact artifact : depArtifacts ) - { - versionsBuffer.append( artifact.getVersion() ).append( File.pathSeparator ); + for (Artifact artifact : depArtifacts) { + versionsBuffer.append(artifact.getVersion()).append(File.pathSeparator); } - antProject.setProperty( versionsPropertyName, versionsBuffer.toString() ); + antProject.setProperty(versionsPropertyName, versionsBuffer.toString()); } /** @@ -490,44 +456,38 @@ public class AntRunMojo * @param mavenProject not null * @since 1.7 */ - public void copyProperties( Project antProject, MavenProject mavenProject ) - { - if ( !exportAntProperties ) - { + public void copyProperties(Project antProject, MavenProject mavenProject) { + if (!exportAntProperties) { return; } - getLog().debug( "Propagated Ant properties to Maven properties" ); + getLog().debug("Propagated Ant properties to Maven properties"); Hashtable<String, Object> antProps = antProject.getProperties(); Properties mavenProperties = mavenProject.getProperties(); - for ( Map.Entry<String, Object> entry : antProps.entrySet() ) - { + for (Map.Entry<String, Object> entry : antProps.entrySet()) { String key = entry.getKey(); - if ( mavenProperties.getProperty( key ) != null ) - { - getLog().debug( "Ant property '" + key + "=" + mavenProperties.getProperty( key ) - + "' clashs with an existing Maven property, SKIPPING this Ant property propagation." ); + if (mavenProperties.getProperty(key) != null) { + getLog().debug("Ant property '" + key + "=" + mavenProperties.getProperty(key) + + "' clashs with an existing Maven property, SKIPPING this Ant property propagation."); continue; } // it is safe to call toString directly since the value cannot be null in Hashtable - mavenProperties.setProperty( key, entry.getValue().toString() ); + mavenProperties.setProperty(key, entry.getValue().toString()); } } /** * @param antProject {@link Project} */ - public void initMavenTasks( Project antProject ) - { - getLog().debug( "Initialize Maven Ant Tasks" ); + public void initMavenTasks(Project antProject) { + getLog().debug("Initialize Maven Ant Tasks"); Typedef typedef = new Typedef(); - typedef.setProject( antProject ); - typedef.setResource( ANTLIB ); - - if ( getTaskPrefix() != null ) - { - typedef.setURI( TASK_URI ); + typedef.setProject(antProject); + typedef.setResource(ANTLIB); + + if (getTaskPrefix() != null) { + typedef.setURI(TASK_URI); } typedef.execute(); } @@ -537,39 +497,31 @@ public class AntRunMojo * * @throws IOException problem with write to file */ - private File writeTargetToProjectFile( String targetName ) - throws IOException - { + private File writeTargetToProjectFile(String targetName) throws IOException { // The fileName should probably use the plugin executionId instead of the targetName - File buildFile = new File( mavenProject.getBuild().getDirectory(), "antrun/build-" + targetName + ".xml" ); + File buildFile = new File(mavenProject.getBuild().getDirectory(), "antrun/build-" + targetName + ".xml"); // noinspection ResultOfMethodCallIgnored buildFile.getParentFile().mkdirs(); AntrunXmlPlexusConfigurationWriter xmlWriter = new AntrunXmlPlexusConfigurationWriter(); String taskPrefix = getTaskPrefix(); - if ( taskPrefix != null ) - { + if (taskPrefix != null) { // replace namespace as Ant expects it to be - target.setAttribute( "xmlns:" + taskPrefix, TASK_URI ); + target.setAttribute("xmlns:" + taskPrefix, TASK_URI); } - xmlWriter.write( target, buildFile, "", targetName ); + xmlWriter.write(target, buildFile, "", targetName); return buildFile; } - private String getTaskPrefix() - { + private String getTaskPrefix() { String taskPrefix = this.customTaskPrefix; - if ( taskPrefix == null ) - { - for ( String name : target.getAttributeNames() ) - { - if ( name.startsWith( "xmlns:" ) - && "http://maven.apache.org/ANTRUN".equals( target.getAttribute( name ) ) ) - { - taskPrefix = name.substring( "xmlns:".length() ); + if (taskPrefix == null) { + for (String name : target.getAttributeNames()) { + if (name.startsWith("xmlns:") && "http://maven.apache.org/ANTRUN".equals(target.getAttribute(name))) { + taskPrefix = name.substring("xmlns:".length()); break; } } @@ -582,35 +534,28 @@ public class AntRunMojo * @return the fragment XML part where the buildException occurs. * @since 1.7 */ - private String findFragment( BuildException buildException ) - { - if ( buildException == null || buildException.getLocation() == null - || buildException.getLocation().getFileName() == null ) - { + private String findFragment(BuildException buildException) { + if (buildException == null + || buildException.getLocation() == null + || buildException.getLocation().getFileName() == null) { return null; } - File antFile = new File( buildException.getLocation().getFileName() ); - if ( !antFile.exists() ) - { + File antFile = new File(buildException.getLocation().getFileName()); + if (!antFile.exists()) { return null; } - try ( LineNumberReader reader = new LineNumberReader( ReaderFactory.newXmlReader( antFile ) ) ) - { - for ( String line = reader.readLine(); line != null; line = reader.readLine() ) - { - if ( reader.getLineNumber() == buildException.getLocation().getLineNumber() ) - { - return "around Ant part ..." + line.trim() + "... @ " + buildException.getLocation().getLineNumber() - + ":" + buildException.getLocation().getColumnNumber() + " in " + antFile.getAbsolutePath(); - + try (LineNumberReader reader = new LineNumberReader(ReaderFactory.newXmlReader(antFile))) { + for (String line = reader.readLine(); line != null; line = reader.readLine()) { + if (reader.getLineNumber() == buildException.getLocation().getLineNumber()) { + return "around Ant part ..." + line.trim() + "... @ " + + buildException.getLocation().getLineNumber() + ":" + + buildException.getLocation().getColumnNumber() + " in " + antFile.getAbsolutePath(); } } - } - catch ( Exception e ) - { - getLog().debug( e.getMessage(), e ); + } catch (Exception e) { + getLog().debug(e.getMessage(), e); } return null; diff --git a/src/main/java/org/apache/maven/plugins/antrun/AntrunXmlPlexusConfigurationWriter.java b/src/main/java/org/apache/maven/plugins/antrun/AntrunXmlPlexusConfigurationWriter.java index aee667e..da5f1de 100644 --- a/src/main/java/org/apache/maven/plugins/antrun/AntrunXmlPlexusConfigurationWriter.java +++ b/src/main/java/org/apache/maven/plugins/antrun/AntrunXmlPlexusConfigurationWriter.java @@ -1,5 +1,3 @@ -package org.apache.maven.plugins.antrun; - /* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file @@ -18,11 +16,7 @@ package org.apache.maven.plugins.antrun; * specific language governing permissions and limitations * under the License. */ - -import org.codehaus.plexus.configuration.PlexusConfiguration; -import org.codehaus.plexus.util.xml.Xpp3DomUtils; -import org.codehaus.plexus.util.xml.pull.MXSerializer; -import org.codehaus.plexus.util.xml.pull.XmlSerializer; +package org.apache.maven.plugins.antrun; import java.io.BufferedOutputStream; import java.io.File; @@ -32,15 +26,18 @@ import java.util.Arrays; import java.util.HashSet; import java.util.Set; +import org.codehaus.plexus.configuration.PlexusConfiguration; +import org.codehaus.plexus.util.xml.Xpp3DomUtils; +import org.codehaus.plexus.util.xml.pull.MXSerializer; +import org.codehaus.plexus.util.xml.pull.XmlSerializer; + /** * Write the Ant target Plexus configuration to an XML file. */ -class AntrunXmlPlexusConfigurationWriter -{ +class AntrunXmlPlexusConfigurationWriter { - private static final Set<String> EXCLUDED_ATTRIBUTES = - new HashSet<>( Arrays.asList( Xpp3DomUtils.CHILDREN_COMBINATION_MODE_ATTRIBUTE, - Xpp3DomUtils.SELF_COMBINATION_MODE_ATTRIBUTE ) ); + private static final Set<String> EXCLUDED_ATTRIBUTES = new HashSet<>(Arrays.asList( + Xpp3DomUtils.CHILDREN_COMBINATION_MODE_ATTRIBUTE, Xpp3DomUtils.SELF_COMBINATION_MODE_ATTRIBUTE)); /** * @param configuration {@link PlexusConfiguration} @@ -49,70 +46,55 @@ class AntrunXmlPlexusConfigurationWriter * @param antTargetName Name of the default Ant target. * @throws IOException In case of problems. */ - public void write( PlexusConfiguration configuration, File file, String customTaskPrefix, String antTargetName ) - throws IOException - { + public void write(PlexusConfiguration configuration, File file, String customTaskPrefix, String antTargetName) + throws IOException { MXSerializer serializer = new MXSerializer(); - serializer.setProperty( "http://xmlpull.org/v1/doc/properties.html#serializer-line-separator", - System.getProperty( "line.separator" ) ); - serializer.setProperty( "http://xmlpull.org/v1/doc/properties.html#serializer-indentation", " " ); - try ( BufferedOutputStream bos = new BufferedOutputStream( new FileOutputStream( file ) ) ) - { - serializer.setOutput( bos, AntRunMojo.UTF_8 ); - serializer.startDocument( AntRunMojo.UTF_8, null ); - if ( !customTaskPrefix.isEmpty() ) - { - serializer.setPrefix( customTaskPrefix, AntRunMojo.TASK_URI ); + serializer.setProperty( + "http://xmlpull.org/v1/doc/properties.html#serializer-line-separator", + System.getProperty("line.separator")); + serializer.setProperty("http://xmlpull.org/v1/doc/properties.html#serializer-indentation", " "); + try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file))) { + serializer.setOutput(bos, AntRunMojo.UTF_8); + serializer.startDocument(AntRunMojo.UTF_8, null); + if (!customTaskPrefix.isEmpty()) { + serializer.setPrefix(customTaskPrefix, AntRunMojo.TASK_URI); } - serializer.startTag( null, "project" ); - serializer.attribute( null, "name", "maven-antrun-" ); - serializer.attribute( null, "default", antTargetName ); - write( configuration, serializer ); - serializer.endTag( null, "project" ); + serializer.startTag(null, "project"); + serializer.attribute(null, "name", "maven-antrun-"); + serializer.attribute(null, "default", antTargetName); + write(configuration, serializer); + serializer.endTag(null, "project"); serializer.endDocument(); } } - private void write( PlexusConfiguration c, XmlSerializer serializer ) - throws IOException - { - serializer.startTag( null, c.getName() ); - writeAttributes( c, serializer ); + private void write(PlexusConfiguration c, XmlSerializer serializer) throws IOException { + serializer.startTag(null, c.getName()); + writeAttributes(c, serializer); int count = c.getChildCount(); - if ( count == 0 ) - { + if (count == 0) { String value = c.getValue(); - if ( value != null ) - { - serializer.text( value ); + if (value != null) { + serializer.text(value); } - } - else - { - for ( int i = 0; i < count; i++ ) - { - PlexusConfiguration child = c.getChild( i ); - write( child, serializer ); + } else { + for (int i = 0; i < count; i++) { + PlexusConfiguration child = c.getChild(i); + write(child, serializer); } } - serializer.endTag( null, c.getName() ); + serializer.endTag(null, c.getName()); } - private void writeAttributes( PlexusConfiguration c, XmlSerializer serializer ) - throws IOException - { + private void writeAttributes(PlexusConfiguration c, XmlSerializer serializer) throws IOException { String[] names = c.getAttributeNames(); - for ( String name : names ) - { - if ( !EXCLUDED_ATTRIBUTES.contains( name ) ) - { - serializer.attribute( null, name, c.getAttribute( name ) ); + for (String name : names) { + if (!EXCLUDED_ATTRIBUTES.contains(name)) { + serializer.attribute(null, name, c.getAttribute(name)); } } } - } - diff --git a/src/main/java/org/apache/maven/plugins/antrun/MavenAntRunProject.java b/src/main/java/org/apache/maven/plugins/antrun/MavenAntRunProject.java index 2bedbce..1b6ef5d 100644 --- a/src/main/java/org/apache/maven/plugins/antrun/MavenAntRunProject.java +++ b/src/main/java/org/apache/maven/plugins/antrun/MavenAntRunProject.java @@ -1,5 +1,3 @@ -package org.apache.maven.plugins.antrun; - /* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file @@ -18,6 +16,7 @@ package org.apache.maven.plugins.antrun; * specific language governing permissions and limitations * under the License. */ +package org.apache.maven.plugins.antrun; import org.apache.maven.project.MavenProject; @@ -25,22 +24,18 @@ import org.apache.maven.project.MavenProject; * Encapsulates a Maven project with an unsupported clone operation. This makes sure that, when used as a reference in * an Ant project, it is passed by reference to sub-projects when <code>inheritRefs</code> is set to <code>true</code> * (which would otherwise pass a clone). - * + * * @author gboue */ -public class MavenAntRunProject -{ +public class MavenAntRunProject { private MavenProject mavenProject; - public MavenAntRunProject( MavenProject mavenProject ) - { + public MavenAntRunProject(MavenProject mavenProject) { this.mavenProject = mavenProject; } - public MavenProject getMavenProject() - { + public MavenProject getMavenProject() { return mavenProject; } - } diff --git a/src/main/java/org/apache/maven/plugins/antrun/MavenLogger.java b/src/main/java/org/apache/maven/plugins/antrun/MavenLogger.java index 8a355be..d67bf31 100644 --- a/src/main/java/org/apache/maven/plugins/antrun/MavenLogger.java +++ b/src/main/java/org/apache/maven/plugins/antrun/MavenLogger.java @@ -1,5 +1,3 @@ -package org.apache.maven.plugins.antrun; - /* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file @@ -18,6 +16,7 @@ package org.apache.maven.plugins.antrun; * specific language governing permissions and limitations * under the License. */ +package org.apache.maven.plugins.antrun; import java.io.PrintStream; @@ -28,37 +27,31 @@ import org.apache.tools.ant.Project; /** * Redirects build events from {@link DefaultLogger} to {@link Log}. */ -public class MavenLogger - extends DefaultLogger -{ +public class MavenLogger extends DefaultLogger { private final Log log; - public MavenLogger( Log log ) - { + public MavenLogger(Log log) { this.log = log; } @Override - protected void printMessage( final String message, final PrintStream stream, final int priority ) - { - switch ( priority ) - { + protected void printMessage(final String message, final PrintStream stream, final int priority) { + switch (priority) { case Project.MSG_ERR: - log.error( message ); + log.error(message); break; case Project.MSG_WARN: - log.warn( message ); + log.warn(message); break; case Project.MSG_DEBUG: case Project.MSG_VERBOSE: - log.debug( message ); + log.debug(message); break; case Project.MSG_INFO: default: - log.info( message ); + log.info(message); break; } } - } diff --git a/src/test/java/org/apache/maven/plugins/antrun/AntrunXmlPlexusConfigurationWriterTest.java b/src/test/java/org/apache/maven/plugins/antrun/AntrunXmlPlexusConfigurationWriterTest.java index 4a7a83b..4d609a3 100644 --- a/src/test/java/org/apache/maven/plugins/antrun/AntrunXmlPlexusConfigurationWriterTest.java +++ b/src/test/java/org/apache/maven/plugins/antrun/AntrunXmlPlexusConfigurationWriterTest.java @@ -1,5 +1,3 @@ -package org.apache.maven.plugins.antrun; - /* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file @@ -18,6 +16,12 @@ package org.apache.maven.plugins.antrun; * specific language governing permissions and limitations * under the License. */ +package org.apache.maven.plugins.antrun; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; import org.codehaus.plexus.configuration.PlexusConfiguration; import org.codehaus.plexus.configuration.xml.XmlPlexusConfiguration; @@ -26,11 +30,6 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; import org.xmlunit.builder.Input; -import java.io.File; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; - import static org.hamcrest.MatcherAssert.assertThat; import static org.xmlunit.matchers.CompareMatcher.isIdenticalTo; @@ -38,8 +37,7 @@ import static org.xmlunit.matchers.CompareMatcher.isIdenticalTo; * Test class for {@link AntrunXmlPlexusConfigurationWriter}. * @author gboue */ -public class AntrunXmlPlexusConfigurationWriterTest -{ +public class AntrunXmlPlexusConfigurationWriterTest { private static final String TARGET_NAME = "main"; @@ -53,12 +51,10 @@ public class AntrunXmlPlexusConfigurationWriterTest private File file; @BeforeEach - void setUp() - throws IOException - { + void setUp() throws IOException { configurationWriter = new AntrunXmlPlexusConfigurationWriter(); - configuration = new XmlPlexusConfiguration( "target" ); - configuration.setAttribute( "name", TARGET_NAME ); + configuration = new XmlPlexusConfiguration("target"); + configuration.setAttribute("name", TARGET_NAME); file = Files.createTempFile(folder, "junit", "antrun").toFile(); } @@ -68,11 +64,10 @@ public class AntrunXmlPlexusConfigurationWriterTest * @throws IOException In case of problems */ @Test - public void testBasic() throws IOException - { - configuration.getChild( "echo", true ).setAttribute( "message", "Hello" ); - configurationWriter.write( configuration, file, "", TARGET_NAME ); - assertXmlIsExpected( "/configuration-writer/basic.xml", file ); + public void testBasic() throws IOException { + configuration.getChild("echo", true).setAttribute("message", "Hello"); + configurationWriter.write(configuration, file, "", TARGET_NAME); + assertXmlIsExpected("/configuration-writer/basic.xml", file); } /** @@ -81,10 +76,9 @@ public class AntrunXmlPlexusConfigurationWriterTest * @throws IOException In case of problems */ @Test - public void testEmptyTarget() throws IOException - { - configurationWriter.write( configuration, file, "", TARGET_NAME ); - assertXmlIsExpected( "/configuration-writer/empty-target.xml", file ); + public void testEmptyTarget() throws IOException { + configurationWriter.write(configuration, file, "", TARGET_NAME); + assertXmlIsExpected("/configuration-writer/empty-target.xml", file); } /** @@ -93,16 +87,15 @@ public class AntrunXmlPlexusConfigurationWriterTest * @throws IOException In case of problems */ @Test - public void testCustomTaskPrefix() throws IOException - { - PlexusConfiguration child = configuration.getChild( "mvn:foo", true ); - child.setAttribute( "attr1", "val1" ); - child.setValue( "The first value." ); - child = configuration.getChild( "bar", true ); - child.setAttribute( "attr2", "val2" ); - child.setValue( "The second value." ); - configurationWriter.write( configuration, file, "mvn", TARGET_NAME ); - assertXmlIsExpected( "/configuration-writer/custom-task-prefix.xml", file ); + public void testCustomTaskPrefix() throws IOException { + PlexusConfiguration child = configuration.getChild("mvn:foo", true); + child.setAttribute("attr1", "val1"); + child.setValue("The first value."); + child = configuration.getChild("bar", true); + child.setAttribute("attr2", "val2"); + child.setValue("The second value."); + configurationWriter.write(configuration, file, "mvn", TARGET_NAME); + assertXmlIsExpected("/configuration-writer/custom-task-prefix.xml", file); } /** @@ -112,17 +105,15 @@ public class AntrunXmlPlexusConfigurationWriterTest * @throws IOException In case of problems */ @Test - public void testCombineAttributes() throws IOException - { - configuration.setAttribute( "combine.children", "append" ); - configuration.setAttribute( "description", "foo" ); - configuration.getChild( "child", true ).setAttribute( "combine.self", "override" ); - configurationWriter.write( configuration, file, "", TARGET_NAME ); - assertXmlIsExpected( "/configuration-writer/combine-attributes.xml", file ); + public void testCombineAttributes() throws IOException { + configuration.setAttribute("combine.children", "append"); + configuration.setAttribute("description", "foo"); + configuration.getChild("child", true).setAttribute("combine.self", "override"); + configurationWriter.write(configuration, file, "", TARGET_NAME); + assertXmlIsExpected("/configuration-writer/combine-attributes.xml", file); } - private void assertXmlIsExpected( String expected, File file ) - { - assertThat( Input.from( file ), isIdenticalTo( Input.from( getClass().getResourceAsStream( expected ) ) ) ); + private void assertXmlIsExpected(String expected, File file) { + assertThat(Input.from(file), isIdenticalTo(Input.from(getClass().getResourceAsStream(expected)))); } }