File Coverage

blib/lib/Maven/MvnAgent.pm
Criterion Covered Total %
statement 9 9 100.0
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 12 12 100.0


line stmt bran cond sub pod time code
1 1     1   26368 use strict;
  1         2  
  1         58  
2 1     1   7 use warnings;
  1         2  
  1         81  
3              
4             package Maven::MvnAgent;
5             $Maven::MvnAgent::VERSION = '1.14';
6             # ABSTRACT: An agent for downloading artifacts using the mvn command
7             # PODNAME: Maven::MvnAgent
8              
9 1     1   783 use parent qw(Maven::Agent);
  1         441  
  1         10  
10              
11             use Carp qw(croak);
12             use File::Copy qw(copy);
13             use Log::Any;
14             use Maven::Command qw(
15             mvn_artifact_params
16             mvn_command
17             );
18              
19             my $logger = Log::Any->get_logger();
20              
21             sub _artifact_command {
22              
23             # [\%maven_options] $artifact @_
24             my $self = shift;
25             my $maven_options = ref( $_[0] ) eq 'HASH' ? shift : {};
26             my $artifact = shift;
27              
28             unless ( $artifact->isa('Maven::Artifact') ) {
29             $artifact = Maven::Artifact->new($artifact);
30             }
31              
32             unless ( $maven_options->{'--settings'} ) {
33             my $file = $self->{maven}->dot_m2('settings.xml');
34             $maven_options->{'--settings'} =
35             $^O eq 'cygwin'
36             ? Cygwin::posix_to_win_path($file)
37             : $file;
38             }
39             unless ( $maven_options->{'--global-settings'} ) {
40             my $file = $self->{maven}->m2_home( 'conf', 'settings.xml' );
41             $maven_options->{'--global-settings'} =
42             $^O eq 'cygwin'
43             ? Cygwin::posix_to_win_path($file)
44             : $file;
45             }
46             unless ( $maven_options->{'-Duser.home'} ) {
47             my $path = $self->{maven}->get_property('user.home');
48             $maven_options->{'-Duser.home'} =
49             $^O eq 'cygwin'
50             ? Cygwin::posix_to_win_path($path)
51             : $path;
52             }
53             if ( $self->{mvn_options} ) {
54             foreach my $mvn_option ( keys( %{ $self->{mvn_options} } ) ) {
55             unless ( $maven_options->{$mvn_option} ) {
56             $maven_options->{$mvn_option} =
57             $self->{mvn_options}{$mvn_option};
58             }
59             }
60             }
61              
62             return $self, $maven_options, $artifact, @_;
63             }
64              
65             sub deploy {
66              
67             # [\%maven_options], $artifact, $file, $repository_id, $repository_url, [%options]
68             my ( $self, @args ) = _artifact_command(@_);
69             $self->_run_or_die( $self->deploy_command(@args) );
70             }
71              
72             sub deploy_command {
73              
74             # [\%maven_options], $artifact, $file, $repository_id, $repository_url, [%options]
75             my ( $self, $maven_options, $artifact, $file, $repository_id, $repository_url, %options ) =
76             _artifact_command(@_);
77              
78             my $maven_deploy_plugin_version = $options{maven_deploy_plugin_version} || '2.8.2';
79              
80             return mvn_command(
81             $maven_options,
82             "org.apache.maven.plugins:maven-deploy-plugin:$maven_deploy_plugin_version:deploy-file",
83             { mvn_artifact_params($artifact),
84             file => $^O eq 'cygwin'
85             ? Cygwin::posix_to_win_path($file)
86             : $file,
87             repositoryId => $repository_id,
88             url => $repository_url
89             }
90             );
91             }
92              
93             sub _download_remote {
94             my ( $self, $artifact, $file ) = @_;
95              
96             my $uri = $self->get($artifact)->get_uri();
97             if ($file) {
98             copy( $uri->path(), $file )
99             || croak('failed to copy file $!');
100             }
101             else {
102             $file = $uri->path();
103             }
104              
105             return $file;
106             }
107              
108             sub get {
109              
110             # [\%maven_options], $artifact, [%options]
111             my ( $self, $maven_options, $artifact, %options ) = _artifact_command(@_);
112              
113             $self->_run_or_die( $self->get_command( $maven_options, $artifact, %options ) );
114              
115             return $self->resolve_or_die( $artifact->get_coordinate() );
116             }
117              
118             sub get_command {
119              
120             # [\%maven_options], $artifact, [%options]
121             my ( $self, $maven_options, $artifact, %options ) = _artifact_command(@_);
122              
123             my $maven_dependency_plugin_version = $options{maven_dependency_plugin_version} || '2.10';
124              
125             my @repositories = ();
126             foreach my $repository ( @{ $self->{maven}->get_repositories()->get_repositories() } ) {
127             next if ( $repository->isa('Maven::LocalRepository') );
128             push( @repositories, $repository->get_url() );
129             }
130              
131             return mvn_command(
132             $maven_options,
133             "org.apache.maven.plugins:maven-dependency-plugin:$maven_dependency_plugin_version:get",
134             { mvn_artifact_params($artifact), remoteRepositories => join( ',', @repositories ) }
135             );
136             }
137              
138             sub _init {
139             my ( $self, %options ) = @_;
140              
141             $self->Maven::Agent::_init(%options);
142             $self->{command_runner} = $options{command_runner};
143             $self->{mvn_options} = $options{mvn_options};
144              
145             return $self;
146             }
147              
148             sub install {
149              
150             # [\%maven_options], $artifact, $file, [%options]
151             my ( $self, @args ) = _artifact_command(@_);
152             $self->_run_or_die( $self->install_command(@args) );
153             }
154              
155             sub install_command {
156              
157             # [\%maven_options], $artifact, $file, [%options]
158             my ( $self, $maven_options, $artifact, $file, %options ) = _artifact_command(@_);
159              
160             my $maven_install_plugin_version = $options{maven_install_plugin_version} || '2.5.2';
161              
162             return mvn_command(
163             $maven_options,
164             "org.apache.maven.plugins:maven-install-plugin:$maven_install_plugin_version:install-file",
165             { mvn_artifact_params($artifact),
166             file => $^O eq 'cygwin'
167             ? Cygwin::posix_to_win_path($file)
168             : $file
169             }
170             );
171             }
172              
173             sub _run_or_die {
174             my ( $self, $command ) = @_;
175              
176             if ( $self->{command_runner} ) {
177             &{ $self->{command_runner} }($command);
178             }
179             else {
180             my $output = `$command`;
181             $logger->tracef( "%s\n---- STDOUT ----\n%s\n---- END STDOUT ----", $command, $output );
182             }
183              
184             croak( "Command [$command] failed: " . ( $? >> 8 ) ) if ($?);
185             }
186              
187             1;
188              
189             __END__