2

I'm using a maven-jaxb2-plugin (org.jvnet.jaxb2.maven2) to generate resources and xjb bindings to add annotations to generated beans.

Those annotations have to be included into plugin's classpath so I'm using dependencies section.

If some of those dependencies missing in maven central build fails. How can I add repositories to look for into the plugin?

E.g. this artifact can't be found in maven central

<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-elasticsearch</artifactId>
    <version>1.0.0.M2</version>
</dependency>

But can be found in another repository:

<repository>
    <id>spring-libs-milestone</id>
    <name>Spring Milestone Repository</name>
    <url>http://repo.spring.io/libs-milestone</url>
</repository>

Plugin config:

<plugin>
    <groupId>org.jvnet.jaxb2.maven2</groupId>
    <artifactId>maven-jaxb2-plugin</artifactId>
    <version>0.8.3</version>
    <dependencies>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-mongodb</artifactId>
            <version>${spring.data.mongodb.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-elasticsearch</artifactId>
            <version>1.0.0.M2</version>
        </dependency>
    </dependencies>
    <executions>
        <execution>
            <goals>
                <goal>generate</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <strict>true</strict>
        <verbose>true</verbose>
        <extension>true</extension>
        <removeOldOutput>true</removeOldOutput>
        <schemaDirectory>src/main/resources</schemaDirectory>
        <bindingDirectory>src/main/resources</bindingDirectory>
        <addCompileSourceRoot>true</addCompileSourceRoot>
        <args>
            <arg>-Xannotate</arg>
        </args>
        <plugins>
            <plugin>
                <groupId>org.jvnet.jaxb2_commons</groupId>
                <artifactId>jaxb2-basics-annotate</artifactId>
                <version>0.6.3</version>
            </plugin>
            <plugin>
                <groupId>org.jvnet.jaxb2_commons</groupId>
                <artifactId>jaxb2-basics</artifactId>
                <version>0.6.3</version>
            </plugin>
        </plugins>
    </configuration>
</plugin>

Build example: http://azee.people.yandex.net/job/elastic-template/6/console

Source code: https://github.com/azee/elastic-template/tree/nodeps

Azee
  • 1,809
  • 17
  • 23
  • Of course we could just generate beans without annotations (baseBeans), extend another beans form them and add annotations to real classes. But it doesn't seem right. I'd prefer to generate all within a jaxb plugin. – Azee Apr 02 '14 at 14:53

2 Answers2

4

You need to define a pluginRepository instead of a normal repository.

Plugins and all their dependencies are only resolved from pluginRepositories. This is to separate code and build dependencies.

So add:

<pluginRepositories>
  <pluginRepository>
    <id>spring-libs-milestone</id>
    <name>Spring Milestone Repository</name>
    <url>http://repo.spring.io/libs-milestone</url>
  </pluginRepository>
</pluginRepositories>
blackbuild
  • 5,026
  • 1
  • 23
  • 35
  • That really helped! Thanks! http://azee.people.yandex.net/job/elastic-template/12/ It seems I should read more manuals about maven plugins :) – Azee Apr 02 '14 at 15:21
0

Author of the maven-jaxb2-plugin as well as jaxb2-basics here. My recommendation would be to add your dependency as plugin into configuration rather that as dependency to the maven plugin.

See this test project:

    <plugins>
        <plugin>
            <groupId>org.jvnet.jaxb2.maven2</groupId>
            <artifactId>maven-jaxb2-plugin</artifactId>
            <configuration>
                <extension>true</extension>
                <args>
                    <arg>-Xannotate</arg>
                </args>
                <plugins>
                    <plugin>
                        <groupId>org.jvnet.jaxb2_commons</groupId>
                        <artifactId>jaxb2-basics-annotate</artifactId>
                    </plugin>
                    <plugin>
                        <groupId>org.jvnet.jaxb2_commons</groupId>
                        <artifactId>jaxb2-basics-test-annox-annotations</artifactId>
                    </plugin>
                </plugins>
            </configuration>
        </plugin>
    </plugins>

I believe this to be better than dependencies of the Maven plugin. First, I think the normal repo will used for the artifact resolution in this case. Next, at least in the times of Maven 2.x, Maven created just one instance of each maven plugin. So the first one in the build order defines the dependencies/classpath of the plugin. Providing the dependency artifact in the configuration does not have this problem.

lexicore
  • 42,748
  • 17
  • 132
  • 221
  • Thank you for the hint. But I don't really like the idea of including a dependency as a plugin. In my case I only need annotations classes from artifact to be able to add them to my POJO beans. If I'll add the dependency to the plugin section another developer who will dig into my code will be disoriented. He wouldn't understand why a non-plugin artifact is based in the plugin section... Anyway, thank you for the hint and for your great plugin that I'm using in all my projects! – Azee Apr 05 '14 at 23:35
  • @Azee Thanks for the feedback. I am thinking about adding `dependencies` or something similar to the plugin config. I agree that `plugins` is confusing. – lexicore Apr 07 '14 at 14:42
  • @edwin Thanks, link is fixed. – lexicore Nov 07 '18 at 17:21
  • @edwin No, I don't. – lexicore Nov 12 '18 at 21:28