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 javax.inject.Inject;
23  import javax.inject.Named;
24  import javax.inject.Singleton;
25  
26  import org.apache.maven.plugins.assembly.AssemblerConfigurationSource;
27  import org.apache.maven.plugins.assembly.InvalidAssemblerConfigurationException;
28  import org.apache.maven.plugins.assembly.archive.ArchiveCreationException;
29  import org.apache.maven.plugins.assembly.archive.phase.wrappers.RepoBuilderConfigSourceWrapper;
30  import org.apache.maven.plugins.assembly.archive.phase.wrappers.RepoInfoWrapper;
31  import org.apache.maven.plugins.assembly.archive.task.AddDirectoryTask;
32  import org.apache.maven.plugins.assembly.format.AssemblyFormattingException;
33  import org.apache.maven.plugins.assembly.model.Assembly;
34  import org.apache.maven.plugins.assembly.model.Repository;
35  import org.apache.maven.plugins.assembly.repository.RepositoryAssembler;
36  import org.apache.maven.plugins.assembly.repository.RepositoryAssemblyException;
37  import org.apache.maven.plugins.assembly.repository.RepositoryBuilderConfigSource;
38  import org.apache.maven.plugins.assembly.repository.model.RepositoryInfo;
39  import org.apache.maven.plugins.assembly.utils.AssemblyFormatUtils;
40  import org.apache.maven.plugins.assembly.utils.TypeConversionUtils;
41  import org.codehaus.plexus.archiver.Archiver;
42  import org.slf4j.Logger;
43  import org.slf4j.LoggerFactory;
44  
45  import java.io.File;
46  import java.util.List;
47  
48  import static java.util.Objects.requireNonNull;
49  
50  /**
51   *
52   */
53  @Singleton
54  @Named( "repositories" )
55  public class RepositoryAssemblyPhase implements AssemblyArchiverPhase, PhaseOrder
56  {
57      private static final Logger LOGGER = LoggerFactory.getLogger( RepositoryAssemblyPhase.class );
58  
59      private final RepositoryAssembler repositoryAssembler;
60  
61  
62      @Inject
63      public RepositoryAssemblyPhase( final RepositoryAssembler repositoryAssembler )
64      {
65          this.repositoryAssembler = requireNonNull( 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          if ( !repositoriesList.isEmpty() )
82          {
83              LOGGER.warn( "" );
84              LOGGER.warn( "The <repository> element in assembly descriptor is deprecated," );
85              LOGGER.warn( "support for it will be removed. For details see:" );
86              LOGGER.warn( "https://issues.apache.org/jira/browse/MASSEMBLY-957" );
87              LOGGER.warn( "" );
88          }
89  
90          for ( final Repository repository : repositoriesList )
91          {
92              final String outputDirectory =
93                  AssemblyFormatUtils.getOutputDirectory( repository.getOutputDirectory(), configSource.getFinalName(),
94                                                          configSource, AssemblyFormatUtils.moduleProjectInterpolator(
95                          configSource.getProject() ), AssemblyFormatUtils.artifactProjectInterpolator( null ) );
96  
97              final File repositoryDirectory = new File( tempRoot, outputDirectory );
98  
99              if ( !repositoryDirectory.exists() )
100             {
101                 repositoryDirectory.mkdirs();
102             }
103 
104             try
105             {
106                 LOGGER.debug( "Assembling repository to: " + repositoryDirectory );
107                 repositoryAssembler.buildRemoteRepository( repositoryDirectory, wrap( repository ),
108                                                            wrap( configSource ) );
109                 LOGGER.debug( "Finished assembling repository to: " + repositoryDirectory );
110             }
111             catch ( final RepositoryAssemblyException e )
112             {
113                 throw new ArchiveCreationException( "Failed to assemble repository: " + e.getMessage(), e );
114             }
115 
116             final AddDirectoryTask task = new AddDirectoryTask( repositoryDirectory );
117 
118             final int dirMode = TypeConversionUtils.modeToInt( repository.getDirectoryMode(), LOGGER );
119             if ( dirMode != -1 )
120             {
121                 task.setDirectoryMode( dirMode );
122             }
123 
124             final int fileMode = TypeConversionUtils.modeToInt( repository.getFileMode(), LOGGER );
125             if ( fileMode != -1 )
126             {
127                 task.setFileMode( fileMode );
128             }
129 
130             task.setOutputDirectory( outputDirectory );
131 
132             task.execute( archiver );
133         }
134     }
135 
136     private RepositoryBuilderConfigSource wrap( final AssemblerConfigurationSource configSource )
137     {
138         return new RepoBuilderConfigSourceWrapper( configSource );
139     }
140 
141     private RepositoryInfo wrap( final Repository repository )
142     {
143         return new RepoInfoWrapper( repository );
144     }
145 
146     @Override
147     public int order()
148     {
149         // CHECKSTYLE_OFF: MagicNumber
150         return 50;
151         // CHECKSTYLE_ON: MagicNumber
152     }
153 }
154