/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.camel.maven.packaging;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import org.apache.maven.model.Resource;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.MavenProjectHelper;

/**
 * Analyses the Camel plugins in a project and generates extra descriptor information for easier auto-discovery in Camel.
 *
 * @goal generate-components-list
 * @execute phase="generate-resources"
 */
public class PackageMojo extends AbstractMojo {

    /**
     * The maven project.
     *
     * @parameter property="project"
     * @required
     * @readonly
     */
    protected MavenProject project;

    /**
     * The output directory for generated components file
     *
     * @parameter default-value="${project.build.directory}/generated/camel/components"
     */
    protected File outDir;

    /**
     * Maven ProjectHelper.
     *
     * @component
     * @readonly
     */
    private MavenProjectHelper projectHelper;

    /**
     * Execute goal.
     *
     * @throws MojoExecutionException execution of the main class or one of the
     *                 threads it generated failed.
     * @throws MojoFailureException something bad happened...
     */
    public void execute() throws MojoExecutionException, MojoFailureException {
        File camelMetaDir = new File(outDir, "META-INF/services/org/apache/camel/");

        StringBuilder buffer = new StringBuilder();
        boolean found = false;
        int count = 0;
        for (Resource r : project.getBuild().getResources()) {
            File f = new File(r.getDirectory());
            if (!f.exists()) {
                f = new File(project.getBasedir(), r.getDirectory());
            }
            f = new File(f, "META-INF/services/org/apache/camel/component");
            
            if (f.exists()) {
                File[] files = f.listFiles();
                for (File file : files) {
                    found = true;
                    count++;
                    String name = file.getName();
                    if (buffer.length() > 0) {
                        buffer.append(" ");
                    }
                    buffer.append(name);
                }
            }
        }
        
        if (found) {
            Properties properties = new Properties();
            String names = buffer.toString();
            properties.put("components", names);
            properties.put("groupId", project.getGroupId());
            properties.put("artifactId", project.getArtifactId());
            properties.put("version", project.getVersion());
            properties.put("projectName", project.getName());
            properties.put("projectDescription", project.getDescription());

            camelMetaDir.mkdirs();
            File outFile = new File(camelMetaDir, "component.properties");
            try {
                properties.store(new FileWriter(outFile), "Generated by camel-package-maven-plugin");
                getLog().info("Generated " + outFile + " containing the Camel " + (count > 1 ? "components " : "component ") + names);

                if (projectHelper != null) {
                    List<String> includes = new ArrayList<String>();
                    includes.add("**/component.properties");
                    projectHelper.addResource(this.project, outDir.getPath(), includes, new ArrayList<String>());
                    projectHelper.attachArtifact(this.project, "properties", "camelComponent", outFile);
                }
            } catch (IOException e) {
                throw new MojoExecutionException("Failed to write properties to " + outFile + ". Reason: " + e, e);
            }
        } else {
            getLog().debug("No META-INF/services/org/apache/camel/component directory found. Are you sure you have created a Camel component?");
        }
    }

}

@@ -2491,39 +2491,8 @@
 ();%0A
-        boolean found = false;%0A
     
@@ -2969,101 +2969,124 @@
     
-found = true;%0A                    count++;%0A                    String name = file.getName();%0A
+String name = file.getName();%0A                    if (name.charAt(0) != '.') %7B%0A                        count++;%0A    
     
@@ -3144,32 +3144,36 @@
                 
+    
 buffer.append(%22 
@@ -3188,34 +3188,40 @@
                 
-%7D%0A
+    %7D%0A  
                 
@@ -3216,32 +3216,34 @@
                 
+  
 buffer.append(na
@@ -3243,24 +3243,46 @@
 pend(name);%0A
+                    %7D%0A
             
@@ -3332,21 +3332,25 @@
     if (
-found
+count %3E 0
 ) %7B%0A    

Skip files that start with "." just in case an old version of svn is picked up