Skip to content

Commit c3e415d

Browse files
committed
Update to non deprecated APIs
Co authored using OpenCode.
1 parent 87f9a4b commit c3e415d

4 files changed

Lines changed: 161 additions & 106 deletions

File tree

src/main/java/org/apache/tomcat/maven/plugin/tomcat/AbstractCatalinaMojo.java

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,9 @@
1818
*/
1919
package org.apache.tomcat.maven.plugin.tomcat;
2020

21-
import org.apache.maven.artifact.manager.WagonManager;
2221
import org.apache.maven.plugin.MojoExecutionException;
23-
import org.apache.maven.plugins.annotations.Component;
2422
import org.apache.maven.plugins.annotations.Parameter;
25-
import org.apache.maven.wagon.authentication.AuthenticationInfo;
23+
import org.apache.maven.settings.Server;
2624
import org.apache.tomcat.maven.common.deployer.TomcatManager;
2725
import org.apache.tomcat.maven.common.deployer.TomcatManagerException;
2826

@@ -64,12 +62,6 @@ public abstract class AbstractCatalinaMojo
6462
// Mojo Parameters
6563
// ----------------------------------------------------------------------
6664

67-
/**
68-
* The Maven Wagon manager to use when obtaining server authentication details.
69-
*/
70-
@Component
71-
private WagonManager wagonManager;
72-
7365
/**
7466
* The full URL of the Tomcat manager instance to use.
7567
*/
@@ -185,24 +177,24 @@ protected TomcatManager getManager()
185177
}
186178
else
187179
{
188-
// obtain authenication details for specified server from wagon
189-
AuthenticationInfo info = wagonManager.getAuthenticationInfo( server );
190-
if ( info == null )
180+
// obtain authentication details for specified server from settings
181+
Server s = settings.getServer( server );
182+
if ( s == null )
191183
{
192184
throw new MojoExecutionException(
193185
messagesProvider.getMessage( "AbstractCatalinaMojo.unknownServer", server ) );
194186
}
195187

196188
// derive username
197-
userName = info.getUserName();
189+
userName = s.getUsername();
198190
if ( userName == null )
199191
{
200192
getLog().debug( messagesProvider.getMessage( "AbstractCatalinaMojo.defaultUserName" ) );
201193
userName = DEFAULT_USERNAME;
202194
}
203195

204196
// derive password
205-
password = info.getPassword();
197+
password = s.getPassword();
206198
if ( password == null )
207199
{
208200
getLog().debug( messagesProvider.getMessage( "AbstractCatalinaMojo.defaultPassword" ) );

src/main/java/org/apache/tomcat/maven/plugin/tomcat/run/AbstractExecWarMojo.java

Lines changed: 58 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,8 @@
3838
import org.apache.commons.compress.archivers.jar.JarArchiveEntry;
3939
import org.apache.commons.io.FileUtils;
4040
import org.apache.commons.io.IOUtils;
41-
4241
import org.apache.maven.artifact.Artifact;
43-
import org.apache.maven.artifact.factory.ArtifactFactory;
4442
import org.apache.maven.artifact.repository.ArtifactRepository;
45-
import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
46-
import org.apache.maven.artifact.resolver.ArtifactResolutionException;
47-
import org.apache.maven.artifact.resolver.ArtifactResolver;
4843
import org.apache.maven.model.Dependency;
4944
import org.apache.maven.plugin.MojoExecutionException;
5045
import org.apache.maven.plugin.MojoFailureException;
@@ -59,6 +54,10 @@
5954
import org.codehaus.plexus.archiver.jar.ManifestException;
6055
import org.codehaus.plexus.util.DirectoryScanner;
6156
import org.codehaus.plexus.util.SelectorUtils;
57+
import org.eclipse.aether.RepositorySystem;
58+
import org.eclipse.aether.artifact.DefaultArtifact;
59+
import org.eclipse.aether.resolution.ArtifactRequest;
60+
import org.eclipse.aether.resolution.ArtifactResult;
6261

6362
/**
6463
* @author Olivier Lamy
@@ -121,13 +120,7 @@ public abstract class AbstractExecWarMojo
121120
protected List<WarRunDependency> warRunDependencies;
122121

123122
@Component
124-
protected ArtifactResolver artifactResolver;
125-
126-
/**
127-
* Maven Artifact Factory component.
128-
*/
129-
@Component
130-
protected ArtifactFactory artifactFactory;
123+
protected RepositorySystem repositorySystem;
131124

132125
/**
133126
* Location of the local repository.
@@ -141,6 +134,9 @@ public abstract class AbstractExecWarMojo
141134
@Parameter( defaultValue = "${project.remoteArtifactRepositories}", required = true, readonly = true )
142135
protected List<ArtifactRepository> remoteRepos;
143136

137+
@Parameter( defaultValue = "${session}", readonly = true, required = true )
138+
protected org.apache.maven.execution.MavenSession session;
139+
144140
@Component
145141
protected MavenProjectHelper projectHelper;
146142

@@ -324,13 +320,7 @@ else if ( warRunDependencies != null && !warRunDependencies.isEmpty() )
324320
"Dependency '" + dependency.getGroupId() + "':'" + dependency.getArtifactId()
325321
+ "' does not have version specified" );
326322
}
327-
Artifact artifact = artifactFactory.createArtifactWithClassifier( dependency.getGroupId(), //
328-
dependency.getArtifactId(), //
329-
version, //
330-
dependency.getType(), //
331-
dependency.getClassifier() );
332-
333-
artifactResolver.resolve( artifact, this.remoteRepos, this.local );
323+
Artifact artifact = toMavenArtifact( dependency.getGroupId(), dependency.getArtifactId(), version, dependency.getType(), dependency.getClassifier() );
334324

335325
File warFileToBundle = new File( resolvePluginWorkDir(), artifact.getFile().getName() );
336326
FileUtils.copyFile( artifact.getFile(), warFileToBundle );
@@ -415,14 +405,7 @@ else if ( warRunDependencies != null && !warRunDependencies.isEmpty() )
415405
+ "' does not have version specified" );
416406
}
417407

418-
// String groupId, String artifactId, String version, String scope, String type
419-
Artifact artifact = artifactFactory.createArtifact( dependency.getGroupId(), //
420-
dependency.getArtifactId(), //
421-
version, //
422-
dependency.getScope(), //
423-
dependency.getType() );
424-
425-
artifactResolver.resolve( artifact, this.remoteRepos, this.local );
408+
Artifact artifact = toMavenArtifact( dependency.getGroupId(), dependency.getArtifactId(), version, dependency.getType(), null );
426409
JarFile jarFile = new JarFile( artifact.getFile() );
427410
extractJarToArchive( jarFile, os, this.excludes );
428411
}
@@ -480,7 +463,7 @@ else if ( warRunDependencies != null && !warRunDependencies.isEmpty() )
480463
}
481464

482465
}
483-
catch (ManifestException | IOException | ArtifactNotFoundException | ArtifactResolutionException e )
466+
catch (ManifestException | IOException e )
484467
{
485468
throw new MojoExecutionException( e.getMessage(), e );
486469
}
@@ -662,4 +645,51 @@ protected void extractJarToArchive( JarFile file, ArchiveOutputStream<JarArchive
662645
}
663646
file.close();
664647
}
648+
649+
private Artifact toMavenArtifact( String groupId, String artifactId, String version, String type, String classifier )
650+
throws MojoExecutionException
651+
{
652+
try
653+
{
654+
org.eclipse.aether.artifact.Artifact aetherArtifact = new DefaultArtifact(
655+
groupId,
656+
artifactId,
657+
classifier != null && !classifier.isEmpty() ? classifier : type,
658+
type,
659+
version
660+
);
661+
662+
ArtifactRequest req = new ArtifactRequest();
663+
req.setArtifact( aetherArtifact );
664+
665+
List<org.eclipse.aether.repository.RemoteRepository> aetherRepos = new ArrayList<>();
666+
for ( ArtifactRepository repo : remoteRepos )
667+
{
668+
org.eclipse.aether.repository.RemoteRepository aetherRepo = new org.eclipse.aether.repository.RemoteRepository.Builder(
669+
repo.getId(), "default", repo.getUrl() )
670+
.build();
671+
aetherRepos.add( aetherRepo );
672+
}
673+
req.setRepositories( aetherRepos );
674+
675+
ArtifactResult result = repositorySystem.resolveArtifact( session.getRepositorySession(), req );
676+
org.eclipse.aether.artifact.Artifact resolved = result.getArtifact();
677+
678+
Artifact mavenArtifact = new org.apache.maven.artifact.DefaultArtifact(
679+
groupId,
680+
artifactId,
681+
version,
682+
Artifact.SCOPE_COMPILE,
683+
type,
684+
classifier != null && !classifier.isEmpty() ? classifier : "",
685+
new org.apache.maven.artifact.handler.DefaultArtifactHandler( type )
686+
);
687+
mavenArtifact.setFile( resolved.getFile() );
688+
return mavenArtifact;
689+
}
690+
catch ( Exception e )
691+
{
692+
throw new MojoExecutionException( "Unable to resolve artifact: " + e.getMessage(), e );
693+
}
694+
}
665695
}

src/main/java/org/apache/tomcat/maven/plugin/tomcat/run/AbstractRunMojo.java

Lines changed: 44 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,9 @@
5757
import org.apache.catalina.startup.Tomcat;
5858
import org.apache.catalina.valves.AccessLogValve;
5959
import org.apache.commons.io.IOUtils;
60-
6160
import org.apache.maven.artifact.Artifact;
62-
import org.apache.maven.artifact.factory.ArtifactFactory;
6361
import org.apache.maven.artifact.repository.ArtifactRepository;
64-
import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
65-
import org.apache.maven.artifact.resolver.ArtifactResolutionException;
66-
import org.apache.maven.artifact.resolver.ArtifactResolver;
6762
import org.apache.maven.artifact.resolver.filter.ScopeArtifactFilter;
68-
import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
69-
import org.apache.maven.artifact.versioning.VersionRange;
7063
import org.apache.maven.execution.MavenSession;
7164
import org.apache.maven.plugin.MojoExecutionException;
7265
import org.apache.maven.plugin.MojoFailureException;
@@ -91,6 +84,10 @@
9184
import org.codehaus.plexus.classworlds.realm.DuplicateRealmException;
9285
import org.codehaus.plexus.util.DirectoryScanner;
9386
import org.codehaus.plexus.util.FileUtils;
87+
import org.eclipse.aether.RepositorySystem;
88+
import org.eclipse.aether.artifact.DefaultArtifact;
89+
import org.eclipse.aether.resolution.ArtifactRequest;
90+
import org.eclipse.aether.resolution.ArtifactResult;
9491
import org.w3c.dom.Document;
9592
import org.w3c.dom.NamedNodeMap;
9693
import org.w3c.dom.Node;
@@ -107,23 +104,14 @@ public abstract class AbstractRunMojo
107104
// Mojo Components
108105
// ---------------------------------------------------------------------
109106

110-
/**
111-
* Used to look up Artifacts in the remote repository.
112-
*/
113-
@Component
114-
protected ArtifactFactory factory;
115-
116107
/**
117108
* Location of the local repository.
118109
*/
119110
@Parameter( defaultValue = "${localRepository}", required = true, readonly = true )
120111
private ArtifactRepository local;
121112

122-
/**
123-
* Used to look up Artifacts in the remote repository.
124-
*/
125113
@Component
126-
protected ArtifactResolver resolver;
114+
protected RepositorySystem repositorySystem;
127115

128116
// ----------------------------------------------------------------------
129117
// Mojo Parameters
@@ -1215,21 +1203,21 @@ private void startContainer()
12151203

12161204
portProperties.put( "tomcat.maven.http.port", Integer.toString( connector.getLocalPort() ) );
12171205

1218-
session.getExecutionProperties().put( "tomcat.maven.http.port",
1206+
session.getUserProperties().setProperty( "tomcat.maven.http.port",
12191207
Integer.toString( connector.getLocalPort() ) );
12201208
System.setProperty( "tomcat.maven.http.port", Integer.toString( connector.getLocalPort() ) );
12211209

12221210
if ( httpsConnector != null )
12231211
{
1224-
session.getExecutionProperties().put( "tomcat.maven.https.port",
1212+
session.getUserProperties().setProperty( "tomcat.maven.https.port",
12251213
Integer.toString( httpsConnector.getLocalPort() ) );
12261214
portProperties.put( "tomcat.maven.https.port", Integer.toString( httpsConnector.getLocalPort() ) );
12271215
System.setProperty( "tomcat.maven.https.port", Integer.toString( httpsConnector.getLocalPort() ) );
12281216
}
12291217

12301218
if ( ajpConnector != null )
12311219
{
1232-
session.getExecutionProperties().put( "tomcat.maven.ajp.port",
1220+
session.getUserProperties().setProperty( "tomcat.maven.ajp.port",
12331221
Integer.toString( ajpConnector.getLocalPort() ) );
12341222
portProperties.put( "tomcat.maven.ajp.port", Integer.toString( ajpConnector.getLocalPort() ) );
12351223
System.setProperty( "tomcat.maven.ajp.port", Integer.toString( ajpConnector.getLocalPort() ) );
@@ -1485,46 +1473,50 @@ private void createStaticContext( final Tomcat container, Context context, Host
14851473
protected Artifact getArtifact( Webapp additionalWebapp )
14861474
throws MojoExecutionException
14871475
{
1488-
1489-
Artifact artifact;
1490-
VersionRange vr;
1491-
try
1492-
{
1493-
vr = VersionRange.createFromVersionSpec( additionalWebapp.getVersion() );
1494-
}
1495-
catch ( InvalidVersionSpecificationException e )
1476+
String classifier = additionalWebapp.getClassifier();
1477+
String version = additionalWebapp.getVersion();
1478+
1479+
org.eclipse.aether.artifact.Artifact aetherArtifact = new DefaultArtifact(
1480+
additionalWebapp.getGroupId(),
1481+
additionalWebapp.getArtifactId(),
1482+
classifier != null && !classifier.isEmpty() ? classifier : "",
1483+
additionalWebapp.getType(),
1484+
version
1485+
);
1486+
1487+
ArtifactRequest artifactRequest = new ArtifactRequest();
1488+
artifactRequest.setArtifact( aetherArtifact );
1489+
1490+
List<org.eclipse.aether.repository.RemoteRepository> aetherRepos = new ArrayList<>();
1491+
for ( org.apache.maven.artifact.repository.ArtifactRepository repo : project.getRemoteArtifactRepositories() )
14961492
{
1497-
getLog().warn( "fail to create versionRange from version: " + additionalWebapp.getVersion(), e );
1498-
vr = VersionRange.createFromVersion( additionalWebapp.getVersion() );
1499-
}
1500-
1501-
if ( additionalWebapp.getClassifier() == null || additionalWebapp.getClassifier().isEmpty() )
1502-
{
1503-
artifact =
1504-
factory.createDependencyArtifact( additionalWebapp.getGroupId(), additionalWebapp.getArtifactId(), vr,
1505-
additionalWebapp.getType(), null, Artifact.SCOPE_COMPILE );
1506-
}
1507-
else
1508-
{
1509-
artifact =
1510-
factory.createDependencyArtifact( additionalWebapp.getGroupId(), additionalWebapp.getArtifactId(), vr,
1511-
additionalWebapp.getType(), additionalWebapp.getClassifier(),
1512-
Artifact.SCOPE_COMPILE );
1493+
org.eclipse.aether.repository.RemoteRepository aetherRepo = new org.eclipse.aether.repository.RemoteRepository.Builder(
1494+
repo.getId(), "default", repo.getUrl() )
1495+
.build();
1496+
aetherRepos.add( aetherRepo );
15131497
}
1498+
artifactRequest.setRepositories( aetherRepos );
15141499

15151500
try
15161501
{
1517-
resolver.resolve( artifact, project.getRemoteArtifactRepositories(), this.local );
1502+
ArtifactResult artifactResult = repositorySystem.resolveArtifact( session.getRepositorySession(), artifactRequest );
1503+
org.eclipse.aether.artifact.Artifact resolved = artifactResult.getArtifact();
1504+
1505+
Artifact artifact = new org.apache.maven.artifact.DefaultArtifact(
1506+
additionalWebapp.getGroupId(),
1507+
additionalWebapp.getArtifactId(),
1508+
version,
1509+
Artifact.SCOPE_COMPILE,
1510+
additionalWebapp.getType(),
1511+
classifier != null && !classifier.isEmpty() ? classifier : "",
1512+
new org.apache.maven.artifact.handler.DefaultArtifactHandler( additionalWebapp.getType() )
1513+
);
1514+
artifact.setFile( resolved.getFile() );
1515+
return artifact;
15181516
}
1519-
catch ( ArtifactResolutionException e )
1517+
catch ( Exception e )
15201518
{
15211519
throw new MojoExecutionException( "Unable to resolve artifact.", e );
15221520
}
1523-
catch ( ArtifactNotFoundException e )
1524-
{
1525-
throw new MojoExecutionException( "Unable to find artifact.", e );
1526-
}
1527-
1528-
return artifact;
15291521
}
15301522
}

0 commit comments

Comments
 (0)