Add .tox/ to .gitignore
[odlparent.git] / karaf / karaf-maven-plugin / src / main / java / org / apache / karaf / tooling / features / AddToRepositoryMojo.java
1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements.  See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License.  You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 package org.apache.karaf.tooling.features;
19
20 import java.io.File;
21 import java.util.List;
22 import java.util.Set;
23
24 import org.apache.karaf.features.BundleInfo;
25 import org.apache.karaf.features.Conditional;
26 import org.apache.karaf.features.internal.model.Bundle;
27 import org.apache.karaf.features.internal.model.ConfigFile;
28 import org.apache.karaf.features.internal.model.Feature;
29 import org.apache.karaf.tooling.utils.MavenUtil;
30 import org.apache.maven.artifact.Artifact;
31 import org.apache.maven.plugin.MojoExecutionException;
32 import org.apache.maven.plugin.MojoFailureException;
33 import org.apache.maven.plugins.annotations.LifecyclePhase;
34 import org.apache.maven.plugins.annotations.Mojo;
35 import org.apache.maven.plugins.annotations.Parameter;
36 import org.apache.maven.plugins.annotations.ResolutionScope;
37
38 /**
39  * Add features to a repository directory
40  */
41 @Mojo(name = "features-add-to-repository", defaultPhase = LifecyclePhase.COMPILE, requiresDependencyResolution = ResolutionScope.RUNTIME, threadSafe = true)
42 public class AddToRepositoryMojo extends AbstractFeatureMojo {
43
44     @Parameter(defaultValue = "${project.build.directory}/features-repo")
45     protected File repository;
46
47     /**
48      * If set to true the exported bundles will be directly copied into the repository dir.
49      * If set to false the default maven repository layout will be used
50      */
51     @Parameter
52     private boolean flatRepoLayout;
53
54     @Parameter
55     protected List<CopyFileBasedDescriptor> copyFileBasedDescriptors;
56
57     public void execute() throws MojoExecutionException, MojoFailureException {
58         Set<Feature> featuresSet = resolveFeatures();
59         
60         for (Artifact descriptor : descriptorArtifacts) {
61             copy(descriptor, repository);
62         }
63
64         for (Feature feature : featuresSet) {
65             copyBundlesToDestRepository(feature.getBundle());
66             for(Conditional conditional : feature.getConditional()) {
67                 copyBundlesConditionalToDestRepository(conditional.getBundles());
68             }
69             copyConfigFilesToDestRepository(feature.getConfigfile());
70         }
71         
72         copyFileBasedDescriptorsToDestRepository();
73         
74     }
75
76     private void copyBundlesConditionalToDestRepository(List<? extends BundleInfo> artifactRefsConditional) throws MojoExecutionException {
77         for (BundleInfo artifactRef : artifactRefsConditional) {
78             if (ignoreDependencyFlag || (!ignoreDependencyFlag && !artifactRef.isDependency())) {
79                 Artifact artifact = resourceToArtifact(artifactRef.getLocation(), skipNonMavenProtocols);
80                 // Avoid getting NPE on artifact.getFile in some cases 
81                 resolveArtifact(artifact, remoteRepos);
82                 if (artifact != null) {
83                     copy(artifact, repository);
84                 }
85             }
86         }
87     }
88     
89     private void copyBundlesToDestRepository(List<? extends Bundle> artifactRefs) throws MojoExecutionException {
90         for (Bundle artifactRef : artifactRefs) {
91             Artifact artifact = resourceToArtifact(artifactRef.getLocation(), skipNonMavenProtocols);
92             // Avoid getting NPE on artifact.getFile in some cases 
93             resolveArtifact(artifact, remoteRepos);
94             if (artifact != null) {
95                 copy(artifact, repository);
96             }
97         }
98     }
99
100     private void copyConfigFilesToDestRepository(List<? extends ConfigFile> artifactRefs) throws MojoExecutionException {
101         for (ConfigFile artifactRef : artifactRefs) {
102             Artifact artifact = resourceToArtifact(artifactRef.getLocation(), skipNonMavenProtocols);
103             // Avoid getting NPE on artifact.getFile in some cases
104             resolveArtifact(artifact, remoteRepos);
105             if (artifact != null) {
106                 copy(artifact, repository);
107             }
108         }
109     }
110
111     protected void copy(Artifact artifact, File destRepository) {
112         try {
113             getLog().info("Copying artifact: " + artifact);
114             File destFile = new File(destRepository, getRelativePath(artifact));
115             if (artifact.getFile() == null) {
116                 throw new IllegalStateException("Artifact is not present in local repo."); 
117             }
118             copy(artifact.getFile(), destFile);
119         } catch (Exception e) {
120             getLog().warn("Error copying artifact " + artifact, e);
121         }
122     }
123
124     /**
125      * Get relative path for artifact
126      * TODO consider DefaultRepositoryLayout
127      * @param artifact
128      * @return relative path of the given artifact in a default repo layout
129      */
130     private String getRelativePath(Artifact artifact) {
131         String dir = (this.flatRepoLayout) ? "" : MavenUtil.getDir(artifact);
132         String name = MavenUtil.getFileName(artifact);
133         return dir + name;
134     }
135
136     private void copyFileBasedDescriptorsToDestRepository() {
137         if (copyFileBasedDescriptors != null) {
138             for (CopyFileBasedDescriptor fileBasedDescriptor : copyFileBasedDescriptors) {
139                 File destDir = new File(repository, fileBasedDescriptor.getTargetDirectory());
140                 File destFile = new File(destDir, fileBasedDescriptor.getTargetFileName());
141                 copy(fileBasedDescriptor.getSourceFile(), destFile);
142             }
143         }
144     }
145
146 }