mavenmaven-install-plugin

Install custom maven plugin from local jar into local repository with dependencies


I have a maven plugin, which I have not uploaded to the central repository, but which I want to use in my projects.

What works

I can install the maven plugin like this:

git clone https://github.com/RudolfVonKrugstein/jinja-maven-plugin.git
cd jinja-maven-plugin
mvn install

Then I can use the plugin like this pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project>
    <modelVersion>4.0.0</modelVersion>

    <groupId>de.wintercloud</groupId>
    <artifactId>sample</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>de.wintercloud</groupId>
            <artifactId>jinja-maven-plugin</artifactId>
            <version>1.0</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>de.wintercloud</groupId>
                <artifactId>jinja-maven-plugin</artifactId>
                <version>1.0</version>
                <executions>
                    <execution>
                        <phase>compile</phase>
                        <goals>
                            <goal>renderjinja</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <outputFile>out.txt</outputFile>
                    <templateFile>templ.jinja</templateFile>
                    <varFile>vars.yaml</varFile>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

Here are the other files relevant to the compile:

templ.jinja:

{{ Name }}

vars.yaml:

Name: MyWonderfullName

This works:

> mvn compile
> cat out.txt 

MyName

Nice!

What does not work

Now I am trying to give the plugin as a jar to my colleagues so that they can simple install the jar. The Idea is to do it like this:

git clone https://github.com/RudolfVonKrugstein/jinja-maven-plugin.git
cd jinja-maven-plugin
mvn package
mvn org.apache.maven.plugins:maven-install-plugin:2.5.2:install-file -Dfile=target/jinja-maven-plugin-1.0.jar

When I now do (in the sample project dir)

mvn compile

I get this error:

[ERROR] Failed to execute goal de.wintercloud:jinja-maven-plugin:1.0:renderjinja (default) on project sample: Execution default of goal de.wintercloud:jinja-maven-plugin:1.0:renderjinja failed: A required class was missing while executing de.wintercloud:jinja-maven-plugin:1.0:renderjinja: org/yaml/snakeyaml/Yaml

How can I install the jar so that I can use it as a plugin? It looks to me as if dependencies are missing. Why?


Solution

  • You just hit MINSTALL-110, which is going to be fixed in the next 3.0.0 release of the Maven Install Plugin. The core issue here is that you're installing manually a JAR file with the file parameter, the plugin detects that there is a POM inside, but only keeps the coordinate information (group id, artifact id, packaging and version), not the whole POM. As such, when the POM is installed, the dependencies aren't kept.

    Indeed, if you take a look at the installed POM, you will see

    <?xml version="1.0" encoding="UTF-8"?>
    <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <modelVersion>4.0.0</modelVersion>
      <groupId>de.wintercloud</groupId>
      <artifactId>jinja-maven-plugin</artifactId>
      <version>1.0</version>
      <packaging>maven-plugin</packaging>
      <description>POM was created from install:install-file</description>
    </project>
    

    which shows that the dependencies weren't retained. And if you take a look at the Maven logs in debug mode when running the install-file command, it will show

    [DEBUG] Using META-INF/maven/de.wintercloud/jinja-maven-plugin/pom.xml for groupId, artifactId, packaging and version

    A work-around waiting for version 3.0.0 is to specify the path to the POM file to install, along with the main artifact, by specifying the pomFile parameter. Run the following instead:

    mvn org.apache.maven.plugins:maven-install-plugin:2.5.2:install-file -Dfile=target/jinja-maven-plugin-1.0.jar -DpomFile=pom.xml
    

    Then the full POM will be installed, not a generic stub.

    With this change, the error

    [ERROR] Failed to execute goal de.wintercloud:jinja-maven-plugin:1.0:renderjinja (default) on project sample: Execution default of goal de.wintercloud:jinja-maven-plugin:1.0:renderjinja failed: A required class was missing while executing de.wintercloud:jinja-maven-plugin:1.0:renderjinja: org/yaml/snakeyaml/Yaml

    will not happen anymore. Maven will correctly download the dependencies and use them for the plugin.