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