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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package org.apache.karaf.tooling.features;
21 import java.util.List;
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;
39 * Add features to a repository directory
41 @Mojo(name = "features-add-to-repository", defaultPhase = LifecyclePhase.COMPILE, requiresDependencyResolution = ResolutionScope.RUNTIME, threadSafe = true)
42 public class AddToRepositoryMojo extends AbstractFeatureMojo {
44 @Parameter(defaultValue = "${project.build.directory}/features-repo")
45 protected File repository;
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
52 private boolean flatRepoLayout;
55 protected List<CopyFileBasedDescriptor> copyFileBasedDescriptors;
57 public void execute() throws MojoExecutionException, MojoFailureException {
58 Set<Feature> featuresSet = resolveFeatures();
60 for (Artifact descriptor : descriptorArtifacts) {
61 copy(descriptor, repository);
64 for (Feature feature : featuresSet) {
65 copyBundlesToDestRepository(feature.getBundle());
66 for(Conditional conditional : feature.getConditional()) {
67 copyBundlesConditionalToDestRepository(conditional.getBundles());
69 copyConfigFilesToDestRepository(feature.getConfigfile());
72 copyFileBasedDescriptorsToDestRepository();
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);
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);
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);
111 protected void copy(Artifact artifact, File destRepository) {
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.");
118 copy(artifact.getFile(), destFile);
119 } catch (Exception e) {
120 getLog().warn("Error copying artifact " + artifact, e);
125 * Get relative path for artifact
126 * TODO consider DefaultRepositoryLayout
128 * @return relative path of the given artifact in a default repo layout
130 private String getRelativePath(Artifact artifact) {
131 String dir = (this.flatRepoLayout) ? "" : MavenUtil.getDir(artifact);
132 String name = MavenUtil.getFileName(artifact);
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);