2

I have multiple POMs that inherit a dependency via their parent. The dependency is not to a maven plugin, but another project that I have created:

        <dependency>
            <groupId>com.my.name.group</groupId>
            <artifactId>id-of-my-artifact</artifactId>
            <version>2.3.0-SNAPSHOT</version>
            <scope>compile</scope>
        </dependency>

Now I want to remove this from the parent POM file and instead have the children POMs use a custom maven plugin for the same functionality. In that custom maven plugin (which I will create), I want to call/add above dependency. I have tried the following code in a Mojo of the new maven-plugin, but it is not adding the dependency (and gives compilation error):

    private MavenProject project;

    public void execute() throws MojoExecutionException, MojoFailureException {
        try {

            // adding a dependency
            Dependency dep = new Dependency();
            dep.setArtifactId("id-of-my-artifact");
            dep.setGroupId("com.my.name.group");
            dep.setScope("compile");
            dep.setVersion("2.3.0-SNAPSHOT");

            List<Dependency> lst = new ArrayList<Dependency>();
            lst.add(dep);
            project.setDependencies(lst);

        } catch (Exception ex) {
            getLog().info(String.format("Exception %s: ", ex));
            throw new MojoExecutionException("Exception has occurred" + ex);
        }
    }

Is it possible to do this? If yes, how?

I looked at this related question and the link provided in that. From the external link, it seems that it is not possible. But in that question, OP seems to have figured it out and provided a short answer, but I cannot understand the answer. Could someone please help me out? I am new to Maven.

Community
  • 1
  • 1
dc95
  • 1,319
  • 1
  • 22
  • 44
  • What is the purpose of what you are trying to implement? Maybe I misunderstand a thing but I don't understand the intention you have explained? Can describe what kind of problem you would like to solve with your plugin? – khmarbaise Oct 26 '16 at 06:45
  • @khmarbaise when I don't want to have any kind of parent for all the different POMs I have but still want to add a dependency in all of them without explicitly mentioning it in their POMs, i.e. via a maven-plugin. I know having a single parent for all of them and adding the dependency in the parent would be the best approach, but I want to do this without having parent POM. I am just asking: is it possible? – dc95 Oct 26 '16 at 17:04
  • So you would like to move the dependencies into a plugin instead of giving it explicitly in the pom file which I don't find a good idea. Apart from that I have my doubts, cause the pom files are read at the beginning before a plugin is started or a life cycle is running. So If you really would like to go this path I would suggest to take a look how to implement an extension instead... – khmarbaise Oct 27 '16 at 06:47

0 Answers0