mirror of https://github.com/apache/maven.git
o Revised transfer listener to provide better feedback
git-svn-id: https://svn.apache.org/repos/asf/maven/maven-3/trunk@828026 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
d15b9be902
commit
4e3d791eef
|
@ -27,7 +27,9 @@ public interface ArtifactTransferListener
|
||||||
|
|
||||||
void transferInitiated( ArtifactTransferEvent transferEvent );
|
void transferInitiated( ArtifactTransferEvent transferEvent );
|
||||||
|
|
||||||
void transferProgress( ArtifactTransferEvent transferEvent, byte[] buffer, int length );
|
void transferStarted( ArtifactTransferEvent transferEvent );
|
||||||
|
|
||||||
|
void transferProgress( ArtifactTransferEvent transferEvent, long transferred, byte[] buffer, int offset, int length );
|
||||||
|
|
||||||
void transferCompleted( ArtifactTransferEvent transferEvent );
|
void transferCompleted( ArtifactTransferEvent transferEvent );
|
||||||
|
|
||||||
|
|
|
@ -22,24 +22,88 @@ package org.apache.maven.repository;
|
||||||
public class MavenArtifact
|
public class MavenArtifact
|
||||||
{
|
{
|
||||||
|
|
||||||
|
private String repositoryUrl;
|
||||||
|
|
||||||
private String name;
|
private String name;
|
||||||
|
|
||||||
private long contentLength;
|
private long contentLength;
|
||||||
|
|
||||||
public MavenArtifact( String name, long contentLength )
|
public MavenArtifact( String repositoryUrl, String name, long contentLength )
|
||||||
{
|
{
|
||||||
this.name = name;
|
if ( repositoryUrl == null )
|
||||||
|
{
|
||||||
|
this.repositoryUrl = "";
|
||||||
|
}
|
||||||
|
else if ( !repositoryUrl.endsWith( "/" ) && repositoryUrl.length() > 0 )
|
||||||
|
{
|
||||||
|
this.repositoryUrl = repositoryUrl + '/';
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
this.repositoryUrl = repositoryUrl;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( name == null )
|
||||||
|
{
|
||||||
|
this.name = "";
|
||||||
|
}
|
||||||
|
else if ( name.startsWith( "/" ) )
|
||||||
|
{
|
||||||
|
this.name = name.substring( 1 );
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
this.contentLength = contentLength;
|
this.contentLength = contentLength;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The base URL of the repository, e.g. "http://repo1.maven.org/maven2/". Unless the URL is unknown, it will be
|
||||||
|
* terminated by a trailing slash.
|
||||||
|
*
|
||||||
|
* @return The base URL of the repository or an empty string if unknown, never {@code null}.
|
||||||
|
*/
|
||||||
|
public String getRepositoryUrl()
|
||||||
|
{
|
||||||
|
return repositoryUrl;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The path of the artifact relative to the repository's base URL.
|
||||||
|
*
|
||||||
|
* @return The path of the artifact, never {@code null}.
|
||||||
|
*/
|
||||||
public String getName()
|
public String getName()
|
||||||
{
|
{
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the full URL of the artifact.
|
||||||
|
*
|
||||||
|
* @return The full URL of the artifact, never {@code null}.
|
||||||
|
*/
|
||||||
|
public String getUrl()
|
||||||
|
{
|
||||||
|
return getRepositoryUrl() + getName();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The size of the artifact in bytes.
|
||||||
|
*
|
||||||
|
* @return The of the artifact in bytes or a negative value if unknown.
|
||||||
|
*/
|
||||||
public long getContentLength()
|
public long getContentLength()
|
||||||
{
|
{
|
||||||
return contentLength;
|
return contentLength;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString()
|
||||||
|
{
|
||||||
|
return getUrl();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,11 +19,15 @@ package org.apache.maven.repository.legacy;
|
||||||
* under the License.
|
* under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
import java.util.IdentityHashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
import org.apache.maven.repository.ArtifactTransferEvent;
|
import org.apache.maven.repository.ArtifactTransferEvent;
|
||||||
import org.apache.maven.repository.ArtifactTransferListener;
|
import org.apache.maven.repository.ArtifactTransferListener;
|
||||||
import org.apache.maven.repository.MavenArtifact;
|
import org.apache.maven.repository.MavenArtifact;
|
||||||
import org.apache.maven.wagon.events.TransferEvent;
|
import org.apache.maven.wagon.events.TransferEvent;
|
||||||
import org.apache.maven.wagon.events.TransferListener;
|
import org.apache.maven.wagon.events.TransferListener;
|
||||||
|
import org.apache.maven.wagon.repository.Repository;
|
||||||
import org.apache.maven.wagon.resource.Resource;
|
import org.apache.maven.wagon.resource.Resource;
|
||||||
|
|
||||||
public class TransferListenerAdapter
|
public class TransferListenerAdapter
|
||||||
|
@ -32,6 +36,8 @@ public class TransferListenerAdapter
|
||||||
|
|
||||||
private ArtifactTransferListener listener;
|
private ArtifactTransferListener listener;
|
||||||
|
|
||||||
|
private Map<Resource, Long> transfers;
|
||||||
|
|
||||||
public static TransferListener newAdapter( ArtifactTransferListener listener )
|
public static TransferListener newAdapter( ArtifactTransferListener listener )
|
||||||
{
|
{
|
||||||
if ( listener == null )
|
if ( listener == null )
|
||||||
|
@ -47,6 +53,7 @@ public class TransferListenerAdapter
|
||||||
private TransferListenerAdapter( ArtifactTransferListener listener )
|
private TransferListenerAdapter( ArtifactTransferListener listener )
|
||||||
{
|
{
|
||||||
this.listener = listener;
|
this.listener = listener;
|
||||||
|
this.transfers = new IdentityHashMap<Resource, Long>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void debug( String message )
|
public void debug( String message )
|
||||||
|
@ -55,6 +62,8 @@ public class TransferListenerAdapter
|
||||||
|
|
||||||
public void transferCompleted( TransferEvent transferEvent )
|
public void transferCompleted( TransferEvent transferEvent )
|
||||||
{
|
{
|
||||||
|
transfers.remove( transferEvent.getResource() );
|
||||||
|
|
||||||
listener.transferCompleted( wrap( transferEvent ) );
|
listener.transferCompleted( wrap( transferEvent ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -69,11 +78,23 @@ public class TransferListenerAdapter
|
||||||
|
|
||||||
public void transferProgress( TransferEvent transferEvent, byte[] buffer, int length )
|
public void transferProgress( TransferEvent transferEvent, byte[] buffer, int length )
|
||||||
{
|
{
|
||||||
listener.transferProgress( wrap( transferEvent ), buffer, length );
|
Long transferred = transfers.get( transferEvent.getResource() );
|
||||||
|
if ( transferred == null )
|
||||||
|
{
|
||||||
|
transferred = Long.valueOf( length );
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
transferred = Long.valueOf( transferred.longValue() + length );
|
||||||
|
}
|
||||||
|
transfers.put( transferEvent.getResource(), transferred );
|
||||||
|
|
||||||
|
listener.transferProgress( wrap( transferEvent ), transferred.longValue(), buffer, 0, length );
|
||||||
}
|
}
|
||||||
|
|
||||||
public void transferStarted( TransferEvent transferEvent )
|
public void transferStarted( TransferEvent transferEvent )
|
||||||
{
|
{
|
||||||
|
listener.transferStarted( wrap( transferEvent ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
private ArtifactTransferEvent wrap( TransferEvent event )
|
private ArtifactTransferEvent wrap( TransferEvent event )
|
||||||
|
@ -84,9 +105,9 @@ public class TransferListenerAdapter
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
String wagon = event.getWagon().getRepository().getUrl();
|
String wagon = event.getWagon().getClass().getName();
|
||||||
|
|
||||||
MavenArtifact artifact = wrap( event.getResource() );
|
MavenArtifact artifact = wrap( event.getWagon().getRepository(), event.getResource() );
|
||||||
|
|
||||||
ArtifactTransferEvent evt;
|
ArtifactTransferEvent evt;
|
||||||
if ( event.getException() != null )
|
if ( event.getException() != null )
|
||||||
|
@ -104,7 +125,7 @@ public class TransferListenerAdapter
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private MavenArtifact wrap( Resource resource )
|
private MavenArtifact wrap( Repository repository, Resource resource )
|
||||||
{
|
{
|
||||||
if ( resource == null )
|
if ( resource == null )
|
||||||
{
|
{
|
||||||
|
@ -112,7 +133,7 @@ public class TransferListenerAdapter
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
return new MavenArtifact( resource.getName(), resource.getContentLength() );
|
return new MavenArtifact( repository.getUrl(), resource.getName(), resource.getContentLength() );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -25,8 +25,9 @@ import org.apache.maven.repository.ArtifactTransferListener;
|
||||||
public abstract class AbstractMavenTransferListener
|
public abstract class AbstractMavenTransferListener
|
||||||
implements ArtifactTransferListener
|
implements ArtifactTransferListener
|
||||||
{
|
{
|
||||||
private boolean showChecksumEvents = false;
|
|
||||||
|
private boolean showChecksumEvents;
|
||||||
|
|
||||||
protected boolean showEvent( ArtifactTransferEvent event )
|
protected boolean showEvent( ArtifactTransferEvent event )
|
||||||
{
|
{
|
||||||
if ( event.getResource() == null )
|
if ( event.getResource() == null )
|
||||||
|
@ -54,20 +55,68 @@ public abstract class AbstractMavenTransferListener
|
||||||
if ( !showEvent( transferEvent ) )
|
if ( !showEvent( transferEvent ) )
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
doInitiated( transferEvent );
|
||||||
}
|
}
|
||||||
|
|
||||||
public void transferProgress( ArtifactTransferEvent transferEvent, byte[] buffer, int length )
|
protected void doInitiated( ArtifactTransferEvent transferEvent )
|
||||||
{
|
{
|
||||||
|
String message =
|
||||||
|
transferEvent.getRequestType() == ArtifactTransferEvent.REQUEST_PUT ? "Uploading" : "Downloading";
|
||||||
|
|
||||||
|
System.out.println( message + ": " + transferEvent.getResource().getUrl() );
|
||||||
|
}
|
||||||
|
|
||||||
|
public void transferStarted( ArtifactTransferEvent transferEvent )
|
||||||
|
{
|
||||||
|
if ( !showEvent( transferEvent ) )
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
doStarted( transferEvent );
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void doStarted( ArtifactTransferEvent transferEvent )
|
||||||
|
{
|
||||||
|
// to be overriden by sub classes
|
||||||
|
}
|
||||||
|
|
||||||
|
public void transferProgress( ArtifactTransferEvent transferEvent, long transferred, byte[] buffer, int offset,
|
||||||
|
int length )
|
||||||
|
{
|
||||||
|
if ( !showEvent( transferEvent ) )
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
doProgress( transferEvent, transferred, buffer, offset, length );
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void doProgress( ArtifactTransferEvent transferEvent, long transferred, byte[] buffer, int offset,
|
||||||
|
int length )
|
||||||
|
{
|
||||||
|
// to be overriden by sub classes
|
||||||
}
|
}
|
||||||
|
|
||||||
public void transferCompleted( ArtifactTransferEvent transferEvent )
|
public void transferCompleted( ArtifactTransferEvent transferEvent )
|
||||||
{
|
{
|
||||||
long contentLength = transferEvent.getResource().getContentLength();
|
if ( !showEvent( transferEvent ) )
|
||||||
if ( contentLength != -1 )
|
|
||||||
{
|
{
|
||||||
String type = ( transferEvent.getRequestType() == ArtifactTransferEvent.REQUEST_PUT ? "uploaded" : "downloaded" );
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
doCompleted( transferEvent );
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void doCompleted( ArtifactTransferEvent transferEvent )
|
||||||
|
{
|
||||||
|
long contentLength = transferEvent.getResource().getContentLength();
|
||||||
|
if ( contentLength >= 0 )
|
||||||
|
{
|
||||||
|
String type =
|
||||||
|
( transferEvent.getRequestType() == ArtifactTransferEvent.REQUEST_PUT ? "uploaded" : "downloaded" );
|
||||||
String l = contentLength >= 1024 ? ( contentLength / 1024 ) + "K" : contentLength + "b";
|
String l = contentLength >= 1024 ? ( contentLength / 1024 ) + "K" : contentLength + "b";
|
||||||
System.out.println( l + " " + type );
|
System.out.println( l + " " + type );
|
||||||
}
|
}
|
||||||
|
@ -82,4 +131,5 @@ public abstract class AbstractMavenTransferListener
|
||||||
{
|
{
|
||||||
this.showChecksumEvents = showChecksumEvents;
|
this.showChecksumEvents = showChecksumEvents;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,16 +19,8 @@ package org.apache.maven.cli;
|
||||||
* under the License.
|
* under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import org.apache.maven.repository.ArtifactTransferEvent;
|
|
||||||
|
|
||||||
public class BatchModeMavenTransferListener
|
public class BatchModeMavenTransferListener
|
||||||
extends AbstractMavenTransferListener
|
extends AbstractMavenTransferListener
|
||||||
{
|
{
|
||||||
public void transferInitiated( ArtifactTransferEvent transferEvent )
|
|
||||||
{
|
|
||||||
if ( !showEvent( transferEvent ) )
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -29,48 +29,23 @@ import org.apache.maven.repository.ArtifactTransferEvent;
|
||||||
public class ConsoleMavenTransferListener
|
public class ConsoleMavenTransferListener
|
||||||
extends AbstractMavenTransferListener
|
extends AbstractMavenTransferListener
|
||||||
{
|
{
|
||||||
private long complete;
|
|
||||||
|
|
||||||
public void transferInitiated( ArtifactTransferEvent transferEvent )
|
@Override
|
||||||
{
|
protected void doProgress( ArtifactTransferEvent transferEvent, long transferred, byte[] buffer, int offset,
|
||||||
super.transferInitiated( transferEvent );
|
int length )
|
||||||
|
|
||||||
complete = 0;
|
|
||||||
|
|
||||||
if ( !showEvent( transferEvent ) )
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
String message =
|
|
||||||
transferEvent.getRequestType() == ArtifactTransferEvent.REQUEST_PUT ? "Uploading" : "Downloading";
|
|
||||||
|
|
||||||
String url = transferEvent.getSource().toString();
|
|
||||||
|
|
||||||
System.out.println( message + ": " + url + "/" + transferEvent.getResource().getName() );
|
|
||||||
}
|
|
||||||
|
|
||||||
public void transferProgress( ArtifactTransferEvent transferEvent, byte[] buffer, int length )
|
|
||||||
{
|
{
|
||||||
long total = transferEvent.getResource().getContentLength();
|
long total = transferEvent.getResource().getContentLength();
|
||||||
complete += length;
|
long complete = transferred;
|
||||||
|
|
||||||
if ( !showEvent( transferEvent ) )
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO [BP]: Sys.out may no longer be appropriate, but will \r work with getLogger()?
|
// TODO [BP]: Sys.out may no longer be appropriate, but will \r work with getLogger()?
|
||||||
if ( total >= 1024 )
|
if ( total >= 1024 )
|
||||||
{
|
{
|
||||||
System.out.print(
|
System.out.print( ( complete / 1024 ) + "/" + ( total == -1 ? "?" : ( total / 1024 ) + "K" ) + "\r" );
|
||||||
( complete / 1024 ) + "/" + ( total == -1 ? "?" : ( total / 1024 ) + "K" )
|
|
||||||
+ "\r" );
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
System.out.print( complete + "/" + ( total == -1 ? "?" : total + "b" ) + "\r" );
|
System.out.print( complete + "/" + ( total == -1 ? "?" : total + "b" ) + "\r" );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue