Skip to content

Commit

Permalink
[MNG-6825] Get rid of commons-lang
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet committed Apr 4, 2023
1 parent 8421a36 commit db428b5
Show file tree
Hide file tree
Showing 26 changed files with 150 additions and 92 deletions.
7 changes: 1 addition & 6 deletions maven-artifact/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,7 @@ under the License.

<name>Maven Artifact</name>

<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
</dependencies>
<dependencies />

<build>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import java.util.Map;
import java.util.regex.Matcher;

import org.apache.commons.lang3.Validate;
import org.apache.maven.artifact.versioning.VersionRange;

/**
Expand Down Expand Up @@ -89,10 +88,15 @@ public static String key(String groupId, String artifactId, String version) {
}

private static void notBlank(String str, String message) {
int c = str != null && str.length() > 0 ? str.charAt(0) : 0;
if ((c < '0' || c > '9') && (c < 'a' || c > 'z')) {
Validate.notBlank(str, message);
final int strLen = str != null ? str.length() : 0;
if (strLen > 0) {
for (int i = 0; i < strLen; i++) {
if (!Character.isWhitespace(str.charAt(i))) {
return;
}
}
}
throw new IllegalArgumentException(message);
}

public static Map<String, Artifact> artifactMapByVersionlessId(Collection<Artifact> artifacts) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@

import java.util.StringTokenizer;

import static org.apache.commons.lang3.math.NumberUtils.isDigits;

/**
* Default implementation of artifact versioning.
*
Expand Down Expand Up @@ -168,6 +166,19 @@ public final void parseVersion(String version) {
}
}

private static boolean isDigits(String cs) {
if (cs == null || cs.isEmpty()) {
return false;
}
final int sz = cs.length();
for (int i = 0; i < sz; i++) {
if (!Character.isDigit(cs.charAt(i))) {
return false;
}
}
return true;
}

private static Integer getNextIntegerToken(StringTokenizer tok) {
String s = tok.nextToken();
if ((s.length() > 1) && s.startsWith("0")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
package org.apache.maven.artifact.repository.metadata;

import java.io.File;
import java.nio.file.Files;
import java.util.Collections;
import java.util.Map;

import org.apache.commons.io.FileUtils;
import org.apache.maven.artifact.metadata.ArtifactMetadata;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.repository.DefaultArtifactRepository;
Expand Down Expand Up @@ -50,7 +50,7 @@ public MetadataBridge(ArtifactMetadata metadata) {
public void merge(File current, File result) throws RepositoryException {
try {
if (current.exists()) {
FileUtils.copyFile(current, result);
Files.copy(current.toPath(), result.toPath());
}
ArtifactRepository localRepo = new MetadataRepository(result);
metadata.storeInLocalRepository(localRepo, localRepo);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@
import javax.inject.Inject;

import java.io.File;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;

import org.apache.commons.io.FileUtils;
import org.apache.maven.artifact.AbstractArtifactComponentTestCase;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.repository.ArtifactRepository;
Expand Down Expand Up @@ -60,15 +61,14 @@ public void testArtifactInstallation() throws Exception {
Artifact artifact = createArtifact("artifact", "1.0");

File file = new File(artifactBasedir, "artifact-1.0.jar");
assertEquals("dummy", FileUtils.readFileToString(file, "UTF-8").trim());
assertEquals("dummy", new String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8).trim());

artifactDeployer.deploy(file, artifact, remoteRepository(), localRepository());

ArtifactRepository remoteRepository = remoteRepository();
File deployedFile = new File(remoteRepository.getBasedir(), remoteRepository.pathOf(artifact));
assertTrue(deployedFile.exists());
assertEquals(
"dummy", FileUtils.readFileToString(deployedFile, "UTF-8").trim());
assertEquals("dummy", new String(Files.readAllBytes(deployedFile.toPath()), StandardCharsets.UTF_8).trim());
} finally {
sessionScope.exit();
}
Expand Down
13 changes: 5 additions & 8 deletions maven-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,6 @@ under the License.
<groupId>com.google.guava</groupId>
<artifactId>failureaccess</artifactId>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
</dependency>
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
Expand All @@ -140,10 +136,6 @@ under the License.
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-classworlds</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
Expand All @@ -158,6 +150,11 @@ under the License.
<artifactId>commons-jxpath</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,12 @@
*/
package org.apache.maven.configuration;

import org.apache.commons.lang3.Validate;
import org.apache.maven.internal.StringUtils;
import org.apache.maven.model.Build;
import org.apache.maven.model.Model;
import org.apache.maven.model.Plugin;
import org.apache.maven.model.PluginExecution;
import org.apache.maven.model.PluginManagement;
import org.codehaus.plexus.util.StringUtils;

/**
* A basic bean configuration request.
Expand Down Expand Up @@ -104,8 +103,8 @@ public DefaultBeanConfigurationRequest setConfiguration(
}

private Plugin findPlugin(Model model, String groupId, String artifactId) {
Validate.notBlank(groupId, "groupId can neither be null, empty nor blank");
Validate.notBlank(artifactId, "artifactId can neither be null, empty nor blank");
StringUtils.notBlank(groupId, "groupId can neither be null, empty nor blank");
StringUtils.notBlank(artifactId, "artifactId can neither be null, empty nor blank");

if (model != null) {
Build build = model.getBuild();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import java.util.Properties;
import java.util.stream.Stream;

import org.apache.commons.lang3.StringUtils;
import org.apache.maven.internal.StringUtils;
import org.apache.maven.project.MavenProject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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
*
* 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
* "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 org.apache.maven.internal;

public class StringUtils {

public static void notBlank(String str, String message) {
if (!isNotBlank(str)) {
throw new IllegalArgumentException(message);
}
}

public static boolean isNotBlank(String str) {
final int strLen = str != null ? str.length() : 0;
if (strLen > 0) {
for (int i = 0; i < strLen; i++) {
if (!Character.isWhitespace(str.charAt(i))) {
return true;
}
}
}
return false;
}

public static boolean isNotEmpty(String str) {
return str != null && !str.isEmpty();
}

public static boolean isEmpty(String str) {
return !isNotEmpty(str);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
import javax.inject.Named;
import javax.inject.Singleton;

import org.apache.commons.lang3.StringUtils;
import org.apache.maven.api.ArtifactCoordinate;
import org.apache.maven.api.annotations.Nonnull;
import org.apache.maven.api.services.ArtifactCoordinateFactory;
import org.apache.maven.api.services.ArtifactCoordinateFactoryRequest;
import org.apache.maven.internal.StringUtils;
import org.eclipse.aether.artifact.ArtifactType;

import static org.apache.maven.internal.impl.Utils.cast;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
import javax.inject.Named;
import javax.inject.Singleton;

import org.apache.commons.lang3.StringUtils;
import org.apache.maven.api.Artifact;
import org.apache.maven.api.annotations.Nonnull;
import org.apache.maven.api.services.ArtifactFactory;
import org.apache.maven.api.services.ArtifactFactoryRequest;
import org.apache.maven.internal.StringUtils;
import org.eclipse.aether.artifact.ArtifactType;

import static org.apache.maven.internal.impl.Utils.cast;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
import javax.inject.Named;
import javax.inject.Singleton;

import org.apache.maven.internal.StringUtils;
import org.apache.maven.plugin.MavenPluginPrerequisitesChecker;
import org.apache.maven.plugin.descriptor.PluginDescriptor;
import org.codehaus.plexus.util.StringUtils;
import org.eclipse.aether.version.InvalidVersionSpecificationException;
import org.eclipse.aether.version.Version;
import org.eclipse.aether.version.VersionConstraint;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;

import org.apache.commons.io.FileUtils;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.metadata.AbstractArtifactMetadata;
import org.apache.maven.artifact.metadata.ArtifactMetadata;
Expand Down Expand Up @@ -74,7 +74,8 @@ public void storeInLocalRepository(ArtifactRepository localRepository, ArtifactR
// ----------------------------------------------------------------------------

try {
FileUtils.copyFile(file, destination);
Files.createDirectories(destination.toPath().getParent());
Files.copy(file.toPath(), destination.toPath());
} catch (IOException e) {
throw new RepositoryMetadataStoreException("Error copying POM to the local repository.", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@
import java.io.InputStream;
import java.util.Properties;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.Validate;
import org.apache.maven.internal.StringUtils;
import org.apache.maven.rtinfo.RuntimeInformation;
import org.eclipse.aether.version.InvalidVersionSpecificationException;
import org.eclipse.aether.version.Version;
Expand Down Expand Up @@ -90,7 +89,7 @@ private String loadMavenVersion() {

@Override
public boolean isMavenVersion(String versionRange) {
Validate.notBlank(versionRange, "versionRange can neither be null, empty nor blank");
StringUtils.notBlank(versionRange, "versionRange can neither be null, empty nor blank");

VersionConstraint constraint;
try {
Expand All @@ -102,7 +101,7 @@ public boolean isMavenVersion(String versionRange) {
Version current;
try {
String mavenVersion = getMavenVersion();
Validate.validState(StringUtils.isNotEmpty(mavenVersion), "Could not determine current Maven version");
StringUtils.notBlank(mavenVersion, "Could not determine current Maven version");

current = versionScheme.parseVersion(mavenVersion);
} catch (InvalidVersionSpecificationException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@
import javax.inject.Inject;

import java.io.File;
import java.nio.file.Files;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.apache.commons.io.FileUtils;
import org.codehaus.plexus.PlexusContainer;
import org.codehaus.plexus.testing.PlexusTest;
import org.junit.jupiter.api.Test;
Expand All @@ -40,7 +42,10 @@ public class ArtifactHandlerTest {
public void testAptConsistency() throws Exception {
File apt = getTestFile("src/site/apt/artifact-handlers.apt");

List<String> lines = FileUtils.readLines(apt);
List<String> lines;
try (Stream<String> l = Files.lines(apt.toPath())) {
lines = l.collect(Collectors.toList());
}

for (String line : lines) {
if (line.startsWith("||")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.FileUtils;
import org.apache.maven.api.model.Model;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.DefaultArtifact;
Expand Down Expand Up @@ -274,7 +274,8 @@ private void resolve(Artifact artifact, ArtifactResolutionRequest request) throw

File remoteFile = new File(remoteRepo.getBasedir(), remoteRepo.pathOf(artifact));

FileUtils.copyFile(remoteFile, localFile);
Files.createDirectories(localFile.toPath().getParent());
Files.copy(remoteFile.toPath(), localFile.toPath());
}

artifact.setResolved(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
package org.apache.maven.project;

import java.io.File;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
Expand Down Expand Up @@ -178,11 +180,11 @@ public void testReadModifiedPoms(@TempDir Path tempDir) throws Exception {
projectBuilder.build(child, configuration);
// modify parent
File parent = new File(tempDir.toFile(), "pom.xml");
String parentContent = FileUtils.readFileToString(parent, "UTF-8");
String parentContent = new String(Files.readAllBytes(parent.toPath()), StandardCharsets.UTF_8);
parentContent = parentContent.replace(
"<packaging>pom</packaging>",
"<packaging>pom</packaging><properties><addedProperty>addedValue</addedProperty></properties>");
FileUtils.write(parent, parentContent, "UTF-8");
Files.write(parent.toPath(), parentContent.getBytes(StandardCharsets.UTF_8));
// re-build pom with modified parent
ProjectBuildingResult result = projectBuilder.build(child, configuration);
assertThat(result.getProject().getProperties(), hasKey((Object) "addedProperty"));
Expand Down
Loading

0 comments on commit db428b5

Please sign in to comment.