View Javadoc
1   package org.apache.maven.plugins.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.plugins.assembly.AssemblerConfigurationSource;
23  import org.apache.maven.plugins.assembly.InvalidAssemblerConfigurationException;
24  import org.apache.maven.plugins.assembly.archive.ArchiveCreationException;
25  import org.apache.maven.plugins.assembly.archive.phase.wrappers.RepoBuilderConfigSourceWrapper;
26  import org.apache.maven.plugins.assembly.archive.phase.wrappers.RepoInfoWrapper;
27  import org.apache.maven.plugins.assembly.archive.task.AddDirectoryTask;
28  import org.apache.maven.plugins.assembly.format.AssemblyFormattingException;
29  import org.apache.maven.plugins.assembly.model.Assembly;
30  import org.apache.maven.plugins.assembly.model.Repository;
31  import org.apache.maven.plugins.assembly.repository.RepositoryAssembler;
32  import org.apache.maven.plugins.assembly.repository.RepositoryAssemblyException;
33  import org.apache.maven.plugins.assembly.repository.RepositoryBuilderConfigSource;
34  import org.apache.maven.plugins.assembly.repository.model.RepositoryInfo;
35  import org.apache.maven.plugins.assembly.utils.AssemblyFormatUtils;
36  import org.apache.maven.plugins.assembly.utils.TypeConversionUtils;
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.html 1016737 2017-08-13 12:01:54Z khmarbaise $
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      @Override
72      @SuppressWarnings( "ResultOfMethodCallIgnored" )
73      public void execute( final Assembly assembly, final Archiver archiver,
74                           final AssemblerConfigurationSource configSource )
75          throws ArchiveCreationException, AssemblyFormattingException, InvalidAssemblerConfigurationException
76      {
77          final List<Repository> repositoriesList = assembly.getRepositories();
78  
79          final File tempRoot = configSource.getTemporaryRootDirectory();
80  
81          for ( final Repository repository : repositoriesList )
82          {
83              final String outputDirectory =
84                  AssemblyFormatUtils.getOutputDirectory( repository.getOutputDirectory(), configSource.getFinalName(),
85                                                          configSource, AssemblyFormatUtils.moduleProjectInterpolator(
86                          configSource.getProject() ), AssemblyFormatUtils.artifactProjectInterpolator( null ) );
87  
88              final File repositoryDirectory = new File( tempRoot, outputDirectory );
89  
90              if ( !repositoryDirectory.exists() )
91              {
92                  repositoryDirectory.mkdirs();
93              }
94  
95              try
96              {
97                  getLogger().debug( "Assembling repository to: " + repositoryDirectory );
98                  repositoryAssembler.buildRemoteRepository( repositoryDirectory, wrap( repository ),
99                                                             wrap( configSource ) );
100                 getLogger().debug( "Finished assembling repository to: " + repositoryDirectory );
101             }
102             catch ( final RepositoryAssemblyException e )
103             {
104                 throw new ArchiveCreationException( "Failed to assemble repository: " + e.getMessage(), e );
105             }
106 
107             final AddDirectoryTask task = new AddDirectoryTask( repositoryDirectory );
108 
109             final int dirMode = TypeConversionUtils.modeToInt( repository.getDirectoryMode(), getLogger() );
110             if ( dirMode != -1 )
111             {
112                 task.setDirectoryMode( dirMode );
113             }
114 
115             final int fileMode = TypeConversionUtils.modeToInt( repository.getFileMode(), getLogger() );
116             if ( fileMode != -1 )
117             {
118                 task.setFileMode( fileMode );
119             }
120 
121             task.setOutputDirectory( outputDirectory );
122 
123             task.execute( archiver );
124         }
125     }
126 
127     private RepositoryBuilderConfigSource wrap( final AssemblerConfigurationSource configSource )
128     {
129         return new RepoBuilderConfigSourceWrapper( configSource );
130     }
131 
132     private RepositoryInfo wrap( final Repository repository )
133     {
134         return new RepoInfoWrapper( repository );
135     }
136 
137     @Override
138     public int order()
139     {
140         // CHECKSTYLE_OFF: MagicNumber
141         return 50;
142         // CHECKSTYLE_ON: MagicNumber
143     }
144 }
145