View Javadoc
1   package org.apache.maven.plugin.assembly.archive.phase;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.plugin.assembly.AssemblerConfigurationSource;
23  import org.apache.maven.plugin.assembly.InvalidAssemblerConfigurationException;
24  import org.apache.maven.plugin.assembly.archive.ArchiveCreationException;
25  import org.apache.maven.plugin.assembly.archive.phase.wrappers.RepoBuilderConfigSourceWrapper;
26  import org.apache.maven.plugin.assembly.archive.phase.wrappers.RepoInfoWrapper;
27  import org.apache.maven.plugin.assembly.archive.task.AddDirectoryTask;
28  import org.apache.maven.plugin.assembly.format.AssemblyFormattingException;
29  import org.apache.maven.plugin.assembly.model.Assembly;
30  import org.apache.maven.plugin.assembly.model.Repository;
31  import org.apache.maven.plugin.assembly.utils.AssemblyFormatUtils;
32  import org.apache.maven.plugin.assembly.utils.TypeConversionUtils;
33  import org.apache.maven.shared.repository.RepositoryAssembler;
34  import org.apache.maven.shared.repository.RepositoryAssemblyException;
35  import org.apache.maven.shared.repository.RepositoryBuilderConfigSource;
36  import org.apache.maven.shared.repository.model.RepositoryInfo;
37  import org.codehaus.plexus.archiver.Archiver;
38  import org.codehaus.plexus.component.annotations.Component;
39  import org.codehaus.plexus.component.annotations.Requirement;
40  import org.codehaus.plexus.logging.AbstractLogEnabled;
41  
42  import java.io.File;
43  import java.util.List;
44  
45  /**
46   * @version $Id: RepositoryAssemblyPhase.java 1640245 2014-11-17 22:23:49Z hboutemy $
47   */
48  @Component( role = AssemblyArchiverPhase.class, hint = "repositories" )
49  public class RepositoryAssemblyPhase
50      extends AbstractLogEnabled
51      implements AssemblyArchiverPhase, PhaseOrder
52  {
53  
54      @Requirement
55      private RepositoryAssembler repositoryAssembler;
56  
57      public RepositoryAssemblyPhase()
58      {
59          // used for plexus.
60      }
61  
62      // introduced for testing.
63      public RepositoryAssemblyPhase( final RepositoryAssembler repositoryAssembler )
64      {
65          this.repositoryAssembler = repositoryAssembler;
66      }
67  
68      /**
69       * {@inheritDoc}
70       */
71      @SuppressWarnings( "ResultOfMethodCallIgnored" )
72      public void execute( final Assembly assembly, final Archiver archiver,
73                           final AssemblerConfigurationSource configSource )
74          throws ArchiveCreationException, AssemblyFormattingException, InvalidAssemblerConfigurationException
75      {
76          final List<Repository> repositoriesList = assembly.getRepositories();
77  
78          final File tempRoot = configSource.getTemporaryRootDirectory();
79  
80          for ( final Repository repository : repositoriesList )
81          {
82              final String outputDirectory =
83                  AssemblyFormatUtils.getOutputDirectory( repository.getOutputDirectory(), configSource.getFinalName(),
84                                                          configSource, AssemblyFormatUtils.moduleProjectInterpolator(
85                          configSource.getProject() ), AssemblyFormatUtils.artifactProjectInterpolator( null ) );
86  
87              final File repositoryDirectory = new File( tempRoot, outputDirectory );
88  
89              if ( !repositoryDirectory.exists() )
90              {
91                  repositoryDirectory.mkdirs();
92              }
93  
94              try
95              {
96                  getLogger().debug( "Assembling repository to: " + repositoryDirectory );
97                  repositoryAssembler.buildRemoteRepository( repositoryDirectory, wrap( repository ),
98                                                             wrap( configSource ) );
99                  getLogger().debug( "Finished assembling repository to: " + repositoryDirectory );
100             }
101             catch ( final RepositoryAssemblyException e )
102             {
103                 throw new ArchiveCreationException( "Failed to assemble repository: " + e.getMessage(), e );
104             }
105 
106             final AddDirectoryTask task = new AddDirectoryTask( repositoryDirectory );
107 
108             final int dirMode = TypeConversionUtils.modeToInt( repository.getDirectoryMode(), getLogger() );
109             if ( dirMode != -1 )
110             {
111                 task.setDirectoryMode( dirMode );
112             }
113 
114             final int fileMode = TypeConversionUtils.modeToInt( repository.getFileMode(), getLogger() );
115             if ( fileMode != -1 )
116             {
117                 task.setFileMode( fileMode );
118             }
119 
120             task.setOutputDirectory( outputDirectory );
121 
122             task.execute( archiver );
123         }
124     }
125 
126     private RepositoryBuilderConfigSource wrap( final AssemblerConfigurationSource configSource )
127     {
128         return new RepoBuilderConfigSourceWrapper( configSource );
129     }
130 
131     private RepositoryInfo wrap( final Repository repository )
132     {
133         return new RepoInfoWrapper( repository );
134     }
135 
136     public int order()
137     {
138         // CHECKSTYLE_OFF: MagicNumber
139         return 50;
140         // CHECKSTYLE_ON: MagicNumber
141     }
142 }
143