package com.artemis;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.apache.maven.project.MavenProject;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import static org.apache.maven.plugins.annotations.LifecyclePhase.GENERATE_SOURCES;

/**
 * The artemis plugin performs bytecode-weaving on annotated components
 * and related classes.
 */
@Mojo(name = "generate", defaultPhase = GENERATE_SOURCES,
        requiresDependencyResolution = ResolutionScope.COMPILE_PLUS_RUNTIME,
        requiresDependencyCollection = ResolutionScope.COMPILE_PLUS_RUNTIME)
public class ArtemisFluidMaven extends AbstractMojo {

    /**
     * Root folder for class files.
     */
    @Parameter(property = "project.build.directory", readonly = true)
    private File outputDirectory;

    /**
     * Root source folder.
     */
    @Parameter(property = "project.build.sourceDirectory", readonly = true)
    private File sourceDirectory;

    private Log log = getLog();

    @Parameter(property = "project.compileClasspathElements", required = true, readonly = true)
    private List<String> classpathElements;

    @Parameter(required = true, property = "project")
    private MavenProject project;

    @Parameter(property = "preferences")
    private MavenFluidGeneratorPreferences preferences = new MavenFluidGeneratorPreferences();

    @Override
    public void execute() throws MojoExecutionException, MojoFailureException {

        prepareGeneratedSourcesFolder();
        includeGeneratedSourcesInCompilation();

        new FluidGenerator().generate(
                classpathAsUrls(preferences),
                generatedSourcesDirectory(), createLogAdapter(), preferences);
    }

    /**
     * bridge maven/internal logging.
     */
    private com.artemis.generator.util.Log createLogAdapter() {
        return new com.artemis.generator.util.Log() {
            @Override
            public void info(String msg) {
                log.info(msg);
            }

            @Override
            public void error(String msg) {
                log.error(msg);
            }
        };
    }

    /**
     * Setup generated sources folder if missing.
     */
    private void prepareGeneratedSourcesFolder() {
        if (!generatedSourcesDirectory().exists() && !generatedSourcesDirectory().mkdirs()) {
            log.error("Could not create " + generatedSourcesDirectory());
        }
    }

    /**
     * Must include manually, or maven buids will fail.
     */
    private void includeGeneratedSourcesInCompilation() {
        this.project.addCompileSourceRoot(generatedSourcesDirectory().getPath());
    }

    private Set<URL> classpathAsUrls(MavenFluidGeneratorPreferences preferences) {
        try {
            Set<URL> urls = new HashSet<URL>();
            for (String element : classpathElements) {
                URL url = new File(element).toURI().toURL();
                if (!preferences.matchesIgnoredClasspath(url.toString())) {
                    urls.add(url);
                    log.info("Including: " + url);
                }
            }
            return urls;
        } catch (MalformedURLException e) {
            throw new RuntimeException("failed to complete classpathAsUrls.", e);
        }
    }

    private File generatedSourcesDirectory() {
        return new File(outputDirectory, "generated-sources/fluid");
    }
}

@@ -15,16 +15,59 @@
 temis;%0A%0A
+import org.apache.maven.artifact.Artifact;%0A
 import o
@@ -99,24 +99,24 @@
 stractMojo;%0A
-
 import org.a
@@ -579,31 +579,8 @@
 et;%0A
-import java.util.List;%0A
 impo
@@ -907,29 +907,16 @@
 .COMPILE
-_PLUS_RUNTIME
 ,%0A      
@@ -975,21 +975,8 @@
 PILE
-_PLUS_RUNTIME
 )%0Apu
@@ -1411,30 +1411,15 @@
 ect.
-compileClasspathElemen
+artifac
 ts%22,
@@ -1469,36 +1469,29 @@
 ate 
-List%3CString%3E classpathElemen
+Set%3CArtifact%3E artifac
 ts;%0A
@@ -3173,40 +3173,35 @@
 or (
-String element : classpathElemen
+Artifact artifact : artifac
 ts) 
@@ -3218,16 +3218,22 @@
         
+final 
 URL url 
@@ -3238,24 +3238,25 @@
 l = 
-new File(element
+artifact.getFile(
 ).to
@@ -3266,24 +3266,81 @@
 ().toURL();%0A
+                if ( artifact.getFile().exists() ) %7B%0A    
             
@@ -3423,16 +3423,20 @@
         
+    
 urls.add
@@ -3438,24 +3438,28 @@
 s.add(url);%0A
+    
             
@@ -3493,24 +3493,129 @@
 : %22 + url);%0A
+                    %7D%0A                %7D else %7B%0A                    log.info(%22Skipping missing: %22 + url);%0A
             

Fix artifact resolution in maven.