如何为maven依赖插件添加额外功能

时间:2015-02-09 12:21:04

标签: java maven

我使用maven-dependency-plugin复制artifactItem的jar文件,而不是在pom.xml中给出artifactItem细节,如下所示

<artifactItem>
                                <groupId>junit</groupId>
                                <artifactId>junit</artifactId>
                                <version>3.8.2</version>
                                <overWrite>true</overWrite>
                                <outputDirectory>${project.build.directory}/alternateLocation</outputDirectory>
                                <destFileName>junit.jar</destFileName>
                            </artifactItem>

我正试图通过使用下面的类来扩展插入以通过progamatically给出artifactItems细节

                package com.nuwaza.aqua.sample.dependencymanager;

            import java.io.File;
            import java.util.ArrayList;
            import java.util.List;

            import org.apache.maven.artifact.Artifact;
            import org.apache.maven.artifact.DefaultArtifact;
            import org.apache.maven.artifact.factory.ArtifactFactory;
            import org.apache.maven.artifact.factory.DefaultArtifactFactory;
            import org.apache.maven.artifact.handler.ArtifactHandler;
            import org.apache.maven.artifact.handler.DefaultArtifactHandler;
            import org.apache.maven.artifact.repository.ArtifactRepository;
            import org.apache.maven.artifact.repository.DefaultArtifactRepository;
            import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
            import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout;
            import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
            import org.apache.maven.artifact.versioning.VersionRange;
            import org.apache.maven.plugin.MojoExecutionException;
            import org.apache.maven.plugin.dependency.fromConfiguration.AbstractFromConfigurationMojo;
            import org.apache.maven.plugin.dependency.fromConfiguration.ArtifactItem;
            import org.apache.maven.plugin.dependency.fromConfiguration.CopyMojo;
            import org.apache.maven.plugin.logging.Log;
            import org.apache.maven.plugins.annotations.Component;
            import org.apache.maven.plugins.annotations.Parameter;
            import org.apache.maven.artifact.versioning.VersionRange;

            /**
             * 
             * @id copy
             * @goal copy
             * @phase package
             * @author anoopab
             */
            public class BundleDataMojo extends CopyMojo {

                @Parameter(required = true)
                private List<ArtifactItem> artifactItems;

                @Component
                protected static ArtifactFactory factory;

                public void execute() throws MojoExecutionException {
                    super.setArtifactItems(setArtifactDetail());

                    super.execute();
                }

                private List<ArtifactItem> setArtifactDetail() {

                    artifactItems = new ArrayList<ArtifactItem>();
                    ArtifactItem bundle = new ArtifactItem();
                    bundle.setGroupId("junit");
                    bundle.setArtifactId("junit");
                    bundle.setVersion("3.8.2");
                    bundle.setDestFileName("samplejunit.jar");
                    bundle.setType("jar");
                    bundle.setOverWrite("true");

                    artifactItems.add(bundle);
                    return artifactItems;
                }
            }

但我得到以下的erorr

[info] Configured Artifact: junit:junit:3.8.2:jar

线程“main”java.lang.NullPointerException中的异常     at org.apache.maven.plugin.dependency.fromConfiguration.AbstractFromConfigurationMojo.getArtifact(AbstractFromConfigurationMojo.java:221)     at org.apache.maven.plugin.dependency.fromConfiguration.AbstractFromConfigurationMojo.getProcessedArtifactItems(AbstractFromConfigurationMojo.java:166)     at org.apache.maven.plugin.dependency.fromConfiguration.CopyMojo.execute(CopyMojo.java:67)     在com.nuwaza.aqua.sample.dependencymanager.BundleDataMojo.execute(BundleDataMojo.java:52)     在com.nuwaza.aqua.sample.dependencymanager.BundleDataMojo.main(BundleDataMojo.java:118)

/**
 * Used to look up Artifacts in the remote repository.
 */
@Component
protected ArtifactFactory factory;

这个工厂得到零值..... org.apache.maven.artifact.factory

中的ArtifactFactory接口

我真的很困惑。任何人都可以提供帮助吗?非常感谢!

0 个答案:

没有答案