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 java.io.File;
23  import java.util.Iterator;
24  import java.util.List;
25  
26  import org.apache.maven.plugin.assembly.AssemblerConfigurationSource;
27  import org.apache.maven.plugin.assembly.AssemblyContext;
28  import org.apache.maven.plugin.assembly.InvalidAssemblerConfigurationException;
29  import org.apache.maven.plugin.assembly.archive.ArchiveCreationException;
30  import org.apache.maven.plugin.assembly.archive.phase.wrappers.RepoBuilderConfigSourceWrapper;
31  import org.apache.maven.plugin.assembly.archive.phase.wrappers.RepoInfoWrapper;
32  import org.apache.maven.plugin.assembly.archive.task.AddDirectoryTask;
33  import org.apache.maven.plugin.assembly.format.AssemblyFormattingException;
34  import org.apache.maven.plugin.assembly.model.Assembly;
35  import org.apache.maven.plugin.assembly.model.Repository;
36  import org.apache.maven.plugin.assembly.utils.AssemblyFormatUtils;
37  import org.apache.maven.plugin.assembly.utils.TypeConversionUtils;
38  import org.apache.maven.shared.repository.RepositoryAssembler;
39  import org.apache.maven.shared.repository.RepositoryAssemblyException;
40  import org.apache.maven.shared.repository.RepositoryBuilderConfigSource;
41  import org.apache.maven.shared.repository.model.RepositoryInfo;
42  import org.codehaus.plexus.archiver.Archiver;
43  import org.codehaus.plexus.component.annotations.Component;
44  import org.codehaus.plexus.component.annotations.Requirement;
45  import org.codehaus.plexus.logging.AbstractLogEnabled;
46  
47  /**
48   * @version $Id: RepositoryAssemblyPhase.java 1517906 2013-08-27 18:25:03Z krosenvold $
49   */
50  @Component( role = AssemblyArchiverPhase.class, hint = "repositories" )
51  public class RepositoryAssemblyPhase
52      extends AbstractLogEnabled
53      implements AssemblyArchiverPhase
54  {
55  
56      @Requirement
57      private RepositoryAssembler repositoryAssembler;
58  
59      public RepositoryAssemblyPhase()
60      {
61          // used for plexus.
62      }
63  
64      // introduced for testing.
65      public RepositoryAssemblyPhase( final RepositoryAssembler repositoryAssembler )
66      {
67          this.repositoryAssembler = repositoryAssembler;
68      }
69  
70      /**
71       * {@inheritDoc}
72       */
73      public void execute( final Assembly assembly, final Archiver archiver,
74                           final AssemblerConfigurationSource configSource, final AssemblyContext context )
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              final String outputDirectory =
83                      AssemblyFormatUtils.getOutputDirectory(repository.getOutputDirectory(), configSource.getProject(),
84                              null, configSource.getFinalName(), configSource);
85  
86              final File repositoryDirectory = new File(tempRoot, outputDirectory);
87  
88              if (!repositoryDirectory.exists()) {
89                  repositoryDirectory.mkdirs();
90              }
91  
92              try {
93                  getLogger().debug("Assembling repository to: " + repositoryDirectory);
94                  repositoryAssembler.buildRemoteRepository(repositoryDirectory, wrap(repository), wrap(configSource));
95                  getLogger().debug("Finished assembling repository to: " + repositoryDirectory);
96              } catch (final RepositoryAssemblyException e) {
97                  throw new ArchiveCreationException("Failed to assemble repository: " + e.getMessage(), e);
98              }
99  
100             final AddDirectoryTask task = new AddDirectoryTask(repositoryDirectory);
101 
102             final int dirMode = TypeConversionUtils.modeToInt(repository.getDirectoryMode(), getLogger());
103             if (dirMode != -1) {
104                 task.setDirectoryMode(dirMode);
105             }
106 
107             final int fileMode = TypeConversionUtils.modeToInt(repository.getFileMode(), getLogger());
108             if (fileMode != -1) {
109                 task.setFileMode(fileMode);
110             }
111 
112             task.setOutputDirectory(outputDirectory);
113 
114             task.execute(archiver, configSource);
115         }
116     }
117 
118     private RepositoryBuilderConfigSource wrap( final AssemblerConfigurationSource configSource )
119     {
120         return new RepoBuilderConfigSourceWrapper( configSource );
121     }
122 
123     private RepositoryInfo wrap( final Repository repository )
124     {
125         return new RepoInfoWrapper( repository );
126     }
127 
128 }