Skip to content

Commit d9c3cb1

Browse files
committed
Switch to Path instead of File
1 parent 8436101 commit d9c3cb1

File tree

5 files changed

+40
-48
lines changed

5 files changed

+40
-48
lines changed

src/it/settings.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,11 @@ under the License.
3232
<url>@localRepositoryUrl@</url>
3333
<releases>
3434
<enabled>true</enabled>
35+
<checksumPolicy>ignore</checksumPolicy>
3536
</releases>
3637
<snapshots>
3738
<enabled>true</enabled>
39+
<checksumPolicy>ignore</checksumPolicy>
3840
</snapshots>
3941
</repository>
4042
</repositories>
@@ -44,9 +46,11 @@ under the License.
4446
<url>@localRepositoryUrl@</url>
4547
<releases>
4648
<enabled>true</enabled>
49+
<checksumPolicy>ignore</checksumPolicy>
4750
</releases>
4851
<snapshots>
4952
<enabled>true</enabled>
53+
<checksumPolicy>ignore</checksumPolicy>
5054
</snapshots>
5155
</pluginRepository>
5256
</pluginRepositories>

src/main/java/org/apache/maven/plugins/deploy/DeployFileMojo.java

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.io.Writer;
2727
import java.nio.file.Files;
2828
import java.nio.file.Path;
29+
import java.nio.file.Paths;
2930
import java.nio.file.StandardCopyOption;
3031
import java.util.ArrayList;
3132
import java.util.List;
@@ -97,23 +98,23 @@ public class DeployFileMojo
9798
* File to be deployed.
9899
*/
99100
@Parameter( property = "file", required = true )
100-
File file;
101+
Path file;
101102

102103
/**
103104
* The bundled API docs for the artifact.
104105
*
105106
* @since 2.6
106107
*/
107108
@Parameter( property = "javadoc" )
108-
File javadoc;
109+
Path javadoc;
109110

110111
/**
111112
* The bundled sources for the artifact.
112113
*
113114
* @since 2.6
114115
*/
115116
@Parameter( property = "sources" )
116-
File sources;
117+
Path sources;
117118

118119
/**
119120
* Server Id to map on the &lt;id&gt; under &lt;server&gt; section of settings.xml In most cases, this parameter
@@ -133,7 +134,7 @@ public class DeployFileMojo
133134
* Location of an existing POM file to be deployed alongside the main artifact, given by the ${file} parameter.
134135
*/
135136
@Parameter( property = "pomFile" )
136-
File pomFile;
137+
Path pomFile;
137138

138139
/**
139140
* Upload a POM for this artifact. Will generate a default POM if none is supplied with the pomFile argument.
@@ -174,15 +175,15 @@ void initProperties()
174175
Path deployedPom;
175176
if ( pomFile != null )
176177
{
177-
deployedPom = pomFile.toPath();
178+
deployedPom = pomFile;
178179
processModel( readModel( deployedPom ) );
179180
}
180181
else
181182
{
182183
deployedPom = readingPomFromJarFile();
183184
if ( deployedPom != null )
184185
{
185-
pomFile = deployedPom.toFile();
186+
pomFile = deployedPom;
186187
}
187188
}
188189

@@ -197,7 +198,7 @@ private Path readingPomFromJarFile()
197198
Pattern pomEntry = Pattern.compile( "META-INF/maven/.*/pom\\.xml" );
198199
try
199200
{
200-
try ( JarFile jarFile = new JarFile( file ) )
201+
try ( JarFile jarFile = new JarFile( file.toFile() ) )
201202
{
202203
JarEntry entry = jarFile.stream()
203204
.filter( e -> pomEntry.matcher( e.getName() ).matches() )
@@ -209,7 +210,7 @@ private Path readingPomFromJarFile()
209210

210211
try ( InputStream pomInputStream = jarFile.getInputStream( entry ) )
211212
{
212-
String base = file.getName();
213+
String base = file.getFileName().toString();
213214
if ( base.indexOf( '.' ) > 0 )
214215
{
215216
base = base.substring( 0, base.lastIndexOf( '.' ) );
@@ -225,7 +226,7 @@ private Path readingPomFromJarFile()
225226
}
226227
else
227228
{
228-
logger.info( "pom.xml not found in " + file.getName() );
229+
logger.info( "pom.xml not found in " + file.getFileName() );
229230
}
230231
}
231232
}
@@ -240,9 +241,9 @@ private Path readingPomFromJarFile()
240241
public void execute()
241242
throws MojoException
242243
{
243-
if ( !file.exists() )
244+
if ( !Files.exists( file ) )
244245
{
245-
String message = "The specified file '" + file.getPath() + "' does not exist";
246+
String message = "The specified file '" + file + "' does not exist";
246247
logger.error( message );
247248
throw new MojoException( message );
248249
}
@@ -259,7 +260,7 @@ public void execute()
259260
Path deployedPom;
260261
if ( pomFile != null )
261262
{
262-
deployedPom = pomFile.toPath();
263+
deployedPom = pomFile;
263264
processModel( readModel( deployedPom ) );
264265
}
265266
else
@@ -287,13 +288,13 @@ public void execute()
287288
Artifact artifact = session.createArtifact( groupId, artifactId, version, classifier,
288289
isFilePom ? "pom" : getExtension( file ), packaging );
289290

290-
if ( file.equals( getLocalRepositoryFile( artifact ).toFile() ) )
291+
if ( file.equals( getLocalRepositoryFile( artifact ) ) )
291292
{
292293
throw new MojoException( "Cannot deploy artifact from the local repository: " + file );
293294
}
294295

295296
ArtifactManager artifactManager = session.getService( ArtifactManager.class );
296-
artifactManager.setPath( artifact, file.toPath() );
297+
artifactManager.setPath( artifact, file );
297298
deployables.add( artifact );
298299

299300

@@ -324,14 +325,14 @@ public void execute()
324325
if ( sources != null )
325326
{
326327
Artifact sourcesArtifact = session.createArtifact( groupId, artifactId, version, "sources", "jar", null );
327-
artifactManager.setPath( sourcesArtifact, sources.toPath() );
328+
artifactManager.setPath( sourcesArtifact, sources );
328329
deployables.add( sourcesArtifact );
329330
}
330331

331332
if ( javadoc != null )
332333
{
333334
Artifact javadocArtifact = session.createArtifact( groupId, artifactId, version, "javadoc", "jar", null );
334-
artifactManager.setPath( javadocArtifact, javadoc.toPath() );
335+
artifactManager.setPath( javadocArtifact, javadoc );
335336
deployables.add( javadocArtifact );
336337
}
337338

@@ -378,21 +379,21 @@ public void execute()
378379
{
379380
nci = classifiers.length();
380381
}
381-
File file = new File( files.substring( fi, nfi ) );
382-
if ( !file.isFile() )
382+
Path file = Paths.get( files.substring( fi, nfi ) );
383+
if ( !Files.isRegularFile( file ) )
383384
{
384385
// try relative to the project basedir just in case
385-
file = new File( files.substring( fi, nfi ) );
386+
file = Paths.get( files.substring( fi, nfi ) );
386387
}
387-
if ( file.isFile() )
388+
if ( Files.isRegularFile( file ) )
388389
{
389390
String extension = getExtension( file );
390391
String type = types.substring( ti, nti ).trim();
391392

392393
Artifact deployable = session.createArtifact(
393394
artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(),
394395
classifiers.substring( ci, nci ).trim(), extension, type );
395-
artifactManager.setPath( deployable, file.toPath() );
396+
artifactManager.setPath( deployable, file );
396397
deployables.add( deployable );
397398
}
398399
else
@@ -583,7 +584,7 @@ void setPackaging( String packaging )
583584
this.packaging = packaging;
584585
}
585586

586-
void setPomFile( File pomFile )
587+
void setPomFile( Path pomFile )
587588
{
588589
this.pomFile = pomFile;
589590
}
@@ -608,7 +609,7 @@ String getPackaging()
608609
return packaging;
609610
}
610611

611-
File getFile()
612+
Path getFile()
612613
{
613614
return file;
614615
}
@@ -640,9 +641,9 @@ private static int countCommas( String str )
640641
/**
641642
* Get file extension, honoring various {@code tar.xxx} combinations.
642643
*/
643-
private String getExtension( final File file )
644+
private String getExtension( final Path file )
644645
{
645-
String filename = file.getName();
646+
String filename = file.getFileName().toString();
646647
int lastDot = filename.lastIndexOf( '.' );
647648
if ( lastDot > 0 && lastDot < filename.length() - 1 )
648649
{

src/test/java/org/apache/maven/plugins/deploy/DeployFileMojoTest.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,9 @@
2626
import static org.junit.jupiter.api.Assertions.assertTrue;
2727
import static org.mockito.ArgumentMatchers.any;
2828
import static org.mockito.Mockito.doAnswer;
29-
import static org.mockito.Mockito.doNothing;
30-
import static org.mockito.Mockito.when;
3129

3230
import java.io.File;
31+
import java.nio.file.Files;
3332
import java.nio.file.Path;
3433
import java.util.ArrayList;
3534
import java.util.List;
@@ -52,7 +51,6 @@
5251
import org.apache.maven.api.plugin.testing.stubs.ArtifactStub;
5352
import org.apache.maven.api.plugin.testing.stubs.SessionStub;
5453
import org.junit.jupiter.api.Test;
55-
import org.mockito.ArgumentCaptor;
5654

5755
/**
5856
* @author <a href="mailto:[email protected]">Allan Ramirez</a>
@@ -89,15 +87,15 @@ public void testBasicDeployFile( DeployFileMojo mojo )
8987
String artifactId = (String) getVariableValueFromObject( mojo, "artifactId" );
9088
String version = (String) getVariableValueFromObject( mojo, "version" );
9189
String packaging = (String) getVariableValueFromObject( mojo, "packaging" );
92-
File file = (File) getVariableValueFromObject( mojo, "file" );
90+
Path file = (Path) getVariableValueFromObject( mojo, "file" );
9391
String repositoryId = (String) getVariableValueFromObject( mojo, "repositoryId" );
9492
String url = (String) getVariableValueFromObject( mojo, "url" );
9593

9694
assertEquals( "org.apache.maven.test", groupId );
9795
assertEquals( "maven-deploy-file-test", artifactId );
9896
assertEquals( "1.0", version );
9997
assertEquals( "jar", packaging );
100-
assertTrue( file.exists() );
98+
assertTrue( Files.exists( file ) );
10199
assertEquals( "deploy-test", repositoryId );
102100
assertEquals( "file://" + getBasedir() + "/target/remote-repo/deploy-file-test", url );
103101

@@ -108,7 +106,7 @@ public void testBasicDeployFile( DeployFileMojo mojo )
108106
assertEquals( 2, artifacts.size() );
109107
Artifact a1 = artifacts.get( 0 );
110108
Path p1 = artifactManager.getPath( a1 ).orElse( null );
111-
assertEquals( file.toPath(), p1 );
109+
assertEquals( file, p1 );
112110
Artifact a2 = artifacts.get( 1 );
113111
Path p2 = artifactManager.getPath( a2 ).orElse( null );
114112
assertNotNull( p2 );

src/test/java/org/apache/maven/plugins/deploy/DeployFileMojoUnitTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
import org.junit.jupiter.api.BeforeEach;
2626
import org.junit.jupiter.api.Test;
2727

28-
import java.io.File;
2928
import java.nio.file.Path;
29+
import java.nio.file.Paths;
3030

3131
import static org.junit.jupiter.api.Assertions.assertEquals;
3232

@@ -68,7 +68,7 @@ protected Model readModel( Path pomFile) throws MojoException {
6868
@Test
6969
public void testProcessPomFromPomFileWithParent4()
7070
{
71-
mojo.setPomFile( new File( "foo.bar" ) );
71+
mojo.setPomFile( Paths.get( "foo.bar" ) );
7272
setMojoModel( mojo, null, "artifact", "version", "packaging", parent );
7373
mojo.initProperties();
7474
checkMojoProperties("parentGroup", "artifact", "version", "packaging");
@@ -77,7 +77,7 @@ public void testProcessPomFromPomFileWithParent4()
7777
@Test
7878
public void testProcessPomFromPomFileWithParent5()
7979
{
80-
mojo.setPomFile( new File( "foo.bar" ) );
80+
mojo.setPomFile( Paths.get( "foo.bar" ) );
8181
setMojoModel( mojo, "group", "artifact", "version", "packaging", parent );
8282
mojo.initProperties();
8383
checkMojoProperties("group", "artifact", "version", "packaging");
@@ -86,7 +86,7 @@ public void testProcessPomFromPomFileWithParent5()
8686
@Test
8787
public void testProcessPomFromPomFileWithParent6()
8888
{
89-
mojo.setPomFile( new File( "foo.bar" ) );
89+
mojo.setPomFile( Paths.get( "foo.bar" ) );
9090
setMojoModel( mojo, "group", "artifact", "version", "packaging", null );
9191
mojo.initProperties();
9292
checkMojoProperties("group", "artifact", "version", "packaging");
@@ -95,7 +95,7 @@ public void testProcessPomFromPomFileWithParent6()
9595
@Test
9696
public void testProcessPomFromPomFileWithOverrides()
9797
{
98-
mojo.setPomFile( new File( "foo.bar" ) );
98+
mojo.setPomFile( Paths.get( "foo.bar" ) );
9999
setMojoModel( mojo, "group", "artifact", "version", "packaging", null );
100100
mojo.setGroupId( "groupO" );
101101
mojo.setArtifactId( "artifactO" );

src/test/java/org/apache/maven/plugins/deploy/DeployMojoTest.java

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,7 @@
5959
import static org.junit.jupiter.api.Assertions.assertThrows;
6060
import static org.junit.jupiter.api.Assertions.assertTrue;
6161
import static org.mockito.Mockito.doNothing;
62-
import static org.mockito.Mockito.doReturn;
63-
import static org.mockito.Mockito.mock;
6462
import static org.mockito.Mockito.spy;
65-
import static org.mockito.Mockito.when;
6663

6764
/**
6865
* @author <a href="mailto:[email protected]">Allan Ramirez</a>
@@ -95,7 +92,7 @@ public void testDeployTestEnvironment( DeployMojo mojo )
9592

9693
@Test
9794
@InjectMojo( goal = "deploy", pom = "classpath:/unit/basic-deploy/plugin-config.xml" )
98-
public void atestBasicDeploy( DeployMojo mojo )
95+
public void testBasicDeploy( DeployMojo mojo )
9996
throws Exception
10097
{
10198
assertNotNull( mojo );
@@ -301,14 +298,6 @@ class TestDeployMojo extends DeployMojo
301298
{
302299
super();
303300
this.session = DeployMojoTest.this.session;
304-
try
305-
{
306-
setVariableValueToObject( this, "session", session );
307-
}
308-
catch ( IllegalAccessException e )
309-
{
310-
throw new IllegalStateException( "Unable to inject session", e );
311-
}
312301
}
313302
}
314303

0 commit comments

Comments
 (0)