Should the wsdlDirectory setting in maven have an effect? - java

Is the wsdlDirectory setting in maven supposed to have an effect? I am finding that the setting:
<wsdlDirectory>${basedir}/src/main/resources/wsdl/</wsdlDirectory>
has no effect.
Executing the command below
mvn -X clean:clean jaxws:wsimport
always results in the output below, unless the wsdl files are moved to /home/projects/amazon/fps/trunk/src/wsdl
[DEBUG] The wsdl Directory is /home/projects/amazon/fps/trunk/src/wsdl
[DEBUG] The binding Directory is
/home/projects/amazon/fps/trunk/src/jaxws
[DEBUG] The wsdl Directory
is /home/projects/amazon/fps/trunk/src/wsdl
[INFO] Nothing to do, no
WSDL found!
I am using 2.2.1 on my Debian build machine and Embedded maven 3.0.2 on my Windows 7 Eclipse environment.
My pom.xml is as follows (irrelevant bits removed):
<project xmlns="..." xmlns:xsi="..." xsi:schemaLocation="...">
<dependencies>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxws-maven-plugin</artifactId>
<executions>
<execution>
<id>AmazonFPSImport</id>
<goals>
<goal>wsimport</goal>
</goals>
<configuration>
<wsdlDirectory>
${basedir}/src/main/resources/wsdl/
</wsdlDirectory>
<wsdlFiles>
<wsdlFile>AmazonFPS.wsdl</wsdlFile>
</wsdlFiles>
<wsdlLocation>/wsdl/AmazonFPS.wsdl</wsdlLocation>
<sourceDestDir>
${basedir}/target/generated-sources/amazon/
</sourceDestDir>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

Try moving the configuration section outside the <execution> tags. Or, bind to a specific phase
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>

Related

How can I use Maven to generate a JAR file from the contents of a directory I specify?

I'm using the maven-deploy-plugin to deploy a third party jar (previously downloaded to my target directory from a plugin not show here) to Nexus as the name 'third-party-1.0.jar', this all works fine using the configuration below.
I also have a javadoc directory in my target directory, which is the javadoc for this third party jar. I'd like to package that javadoc directory as 'third-party-1.0-javadoc.jar'.
If I could get the directory packaged as a JAR, I think I could the javadoc parameter of the deploy plugin below to deploy it, just unsure how to package a custom directory as a JAR with a specific name using Maven, maybe the assembly plugin?
TLDR; How do I use Maven to create a JAR file from the contents of a directory I specify?
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
<executions>
<execution>
<id>default-deploy</id>
<phase>none</phase>
</execution>
<execution>
<id>deploy-jar</id>
<phase>deploy</phase>
<goals>
<goal>deploy-file</goal>
</goals>
<configuration>
<file>${project.build.directory}/code.jar</file>
<url>...</url>
<repositoryId>...</repositoryId>
<url>...</url>
<packaging>jar</packaging>
<generatePom>true</generatePom>
<groupId>com.example</groupId>
<artifactId>third-party</artifactId>
<version>1.0</version>
</configuration>
</execution>
</executions>
</plugin>
You can achieve it by using maven-jar-plugin or by using maven-assembly-plugin
Here is a way to achieve it via maven-jar-plugin
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.1</version>
<executions>
<execution>
<configuration>
<classifier>docs</classifier>
<classesDirectory>${project.build.directory}/docs</classesDirectory>
<includes>**/*</includes>
</configuration>
<id>pack-docs</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
Now run:
mvn clean package
It will create artifact_name-1.0.0-docs.jar jar
How to create addition jar using Maven?
I ended up using the maven-assembly-plugin instead of the maven-jar-plugin as it allows more control over the resulting JAR, including not adding a pom.xml to it, not attaching it to the project as an artifact to be deployed (attach=false), and how it
is named. This allows the maven-deploy-plugin to control how it is deployed, and what Maven coordinates to use when deploying it.
I can then just attach the Javadoc JAR built with the assembly plugin into my deploy file execution using the javadoc tag:
src/main/assembly/assembly.xml
<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd">
<id>package-third-party-javadoc</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>${project.build.directory}/dir</directory>
<outputDirectory>.</outputDirectory>
</fileSet>
</fileSets>
</assembly>
pom.xml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.1</version>
<executions>
<execution>
<id>package-third-party-javadoc</id>
<phase>compile</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptors>
<descriptor>src/main/assembly/assembly.xml</descriptor>
</descriptors>
<finalName>third-party-javadoc</finalName>
<appendAssemblyId>false</appendAssemblyId>
<attach>false</attach>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
<executions>
<execution>
<id>default-deploy</id>
<phase>none</phase>
</execution>
<execution>
<id>deploy-jar</id>
<phase>deploy</phase>
<goals>
<goal>deploy-file</goal>
</goals>
<configuration>
<file>${project.build.directory}/code.jar</file>
<url>...</url>
<repositoryId>...</repositoryId>
<url>...</url>
<packaging>jar</packaging>
<generatePom>true</generatePom>
<groupId>com.example</groupId>
<artifactId>third-party</artifactId>
<version>1.0</version>
<javadoc>${project.build.directory}/third-party-javadoc.jar</javadoc>
</configuration>
</execution>
</executions>
</plugin>

XSD Not Picked Up by XMLBeans Maven Plugin

I am trying to generate XMLBeans classes using maven plugin. I have configured this in my POM file, but every time I do a clean install, I see this in the debug logs:
[DEBUG] Number of XSD Files: 0
[DEBUG] Number of WSDL Files: 0
[INFO] Nothing to generate.
My plugin is defined as:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>xmlbeans-maven-plugin</artifactId>
<version>2.3.3.002</version>
<executions>
<execution>
<id>xsd-to-java-xmlbeans</id>
<phase>generate-sources</phase>
<goals>
<goal>xmlbeans</goal>
</goals>
<inherited>true</inherited>
<configuration>
<classGenerationDirectory>${basedir}\src\main\java\</classGenerationDirectory>
<compiler>1.8</compiler>
<debug>true</debug>
<download>true</download>
<outputJar>subInterfaceXSD.jar</outputJar>
<quiet>false</quiet>
<schemaDirectory>${project.basedir}\src\main\resources\XSDFiles\</schemaDirectory>
<generatedSchemaDirectory>${project.basedir}\src\main\resources\XSDFiles\</generatedSchemaDirectory>
<sourceGenerationDirectory>${basedir}\src\main\java\</sourceGenerationDirectory>
<verbose>true</verbose>
</configuration>
</execution>
</executions>
</plugin>
This was a buggy plugin. Switching to an older version helped.

Maven pom plugin not executing on build

I'm practicing Maven and I've hit a wall. I've installed the PlantUml plugin on IntelliJ and I'm trying to set it up so that it always generates a new image from the source file on compile time. I'm using this plugin to generate the image, and I've configured the pom.xml file as follows:
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>com.github.jeluard</groupId>
<artifactId>plantuml-maven-plugin</artifactId>
<version>1.2</version>
<executions>
<execution>
<id>GeneratePlantUml</id>
<phase>generate-resources</phase>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/images</outputDirectory>
</configuration>
</execution>
</executions>
<configuration>
<sourceFiles>
<directory>${basedir}/plantuml</directory>
<includes>
<include>TestGeneratorDiagram.puml</include>
</includes>
</sourceFiles>
<outputDirectory>${basedir}/images</outputDirectory>
</configuration>
<dependencies>
<dependency>
<groupId>net.sourceforge.plantuml</groupId>
<artifactId>plantuml</artifactId>
<version>8031</version>
</dependency>
</dependencies>
</plugin>
<plugins>
</pluginManagement>
<build>
This works fine when I use a terminal command where I specify the goal:
mvn compile com.github.jeluard:plantuml-maven-plugin:generate
However, it doesn't work if I just write:
mvn compile
Which, as far as I know, should also work. I've tried setting the phase on compile but it didn't change anything. I've searched for hours now for a solution but I haven't found one. Does anyone know how I can force the plugin to generate a new image on compile time by configuring the pom?
You have put your configuration into pluginManagement. You needs to put it into plugins (outside pluginManagement).
The pluginManagement is just to override/specify configuration and version numbers.
Have a look at Maven: What is pluginManagement?
your plugin and your execution is configure à the "generate-resources" phase and not at the compile phase like you want.
See this link to more detail on phase.
change this:
<execution>
<id>GeneratePlantUml</id>
<phase>generate-resources</phase>
<goals>
<goal>generate</goal>
</goals>
to this
<execution>
<id>GeneratePlantUml</id>
<phase>compile</phase>
<goals>
<goal>generate</goal>
</goals>
It must works.

how to specify maven compiler plugin order

I'm working on a mixed java and kotlin project by using maven.
The problem I'm facing right now is, maven-compiler-plugin runs before compiling kotlin-maven-plugin.
[INFO] --- maven-compiler-plugin:3.0:compile (default-compile) #annotation ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 11 source files to /Users/hannes/workspace/tikxml/annotation/target/classes
[INFO] --- kotlin-maven-plugin:1.0.0-beta-4583:compile (compile) # annotation
[INFO] Kotlin Compiler version 1.0.0-beta-4583
In my java source code I'm referencing classes written in kotlin. But javac runs before kotlinc. Hence, maven interrupts with compiler errors.
My pom (parent pom, I use submodules) looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.sonatype.oss</groupId>
<artifactId>oss-parent</artifactId>
<version>7</version>
</parent>
...
<modules>
<module>core</module>
<module>annotation</module>
<module>processor</module>
</modules>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.7</java.version>
<kotlin.version>1.0.0-beta-4583</kotlin.version>
</properties>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin.version}</version>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<sourceDirs>
<source>src/main/java</source>
</sourceDirs>
</configuration>
</execution>
<execution>
<id>test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>test-compile</goal>
</goals>
<configuration>
<sourceDirs>
<source>src/test/java</source>
</sourceDirs>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
The Kotlin documentation on Using Maven suggests explicitly binding the kotlin-maven-plugin execution to the process-sources phase. Since maven-compiler-plugin is bound to the compile phase, binding kotlin-maven-plugin to the process-sources phase makes it run first.
<plugin>
<artifactId>kotlin-maven-plugin</artifactId>
<groupId>org.jetbrains.kotlin</groupId>
<version>${kotlin.version}</version>
<executions>
<execution>
<id>compile</id>
<phase>process-sources</phase>
<goals> <goal>compile</goal> </goals>
</execution>
<execution>
<id>test-compile</id>
<phase>process-test-sources</phase>
<goals> <goal>test-compile</goal> </goals>
</execution>
</executions>
</plugin>
Although it's more verbose, you can compile both Java and Kotlin sources in the compile phase with the following configuration:
<plugin>
<artifactId>kotlin-maven-plugin</artifactId>
<groupId>org.jetbrains.kotlin</groupId>
<version>${kotlin.version}</version>
<executions>
<execution>
<id>kotlin-compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>kotlin-test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5</version>
<executions>
<execution>
<id>default-compile</id>
<phase>none</phase>
</execution>
<execution>
<id>default-testCompile</id>
<phase>none</phase>
</execution>
<execution>
<id>java-compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>java-test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
Normally, plugins are executed in the order they are declared in the POM, but the executions of default-compile and default-testCompile are special because they're built-in, so they always go first. The above configuration gets around that by disabling the default compile executions, and declaring maven-compiler-plugin with new compilation executions after the kotlin-maven-plugin. In this way, you can get all compilation to properly occur during the compile phase of the build lifecycle.
A simple but effective solution would be to change the two phases (the <phase> element) applied to the executions of the Kotlin Maven Plugin, from compile to process-sources and from test-compile to process-test-sources.
You want the Kotlin part to be executed before the Java one. Maven by default will invoke the Maven Compiler Plugin as part of the compile phase for source code, and test-compile for test code. Moving the Kotlin part to their previous phases would then make the flow as you wished.
If you wanted the opposite (first Java then Kotlin), then you could have moved the Kotlin part to the next phases (as an example: from compile to process-classes and from test-compile to process-test-classes).
For further details about Maven phases, check the official documentation.
In case you are open to using a different build system, dependent projects are easy to set up with Kobalt:
val p1 = javaProject { ... }
val p2 = kotlinProject(p1) { ... }
This makes project p2 depend on p1. You can also mix Java and Kotlin projects.

Deploy additional jar file with Maven?

I have an artifact which is being built and deployed in a particular way (not as a jar file). In the course of deployment, a war file is built.
How can I configure the pom so that the artifact is also deployed as a jar file, to a different location?
Maven deploy means deploy artifact to a Maven Repository, not an application server.
Configure additional JAR artifact like this:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<id>make-a-jar</id>
<phase>compile</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
Attach this new artifact to your project:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>attach-artifacts</id>
<phase>package</phase>
<goals>
<goal>attach-artifact</goal>
</goals>
<configuration>
<artifacts>
<artifact>
<file>${project.build.directory}/${project.artifactId}-${project.version}.jar</file>
<!-- <file>${project.build.directory}/${project.build.finalName}.jar</file> - if finalName is defined -->
<type>jar</type>
</artifact>
</artifacts>
</configuration>
</execution>
</executions>
</plugin>
This blog post and its comments have the answer.
These three plugin configurations will allow you to build/install/deploy a jar version alongside the war.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<id>make-a-jar</id>
<phase>compile</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<executions>
<execution>
<phase>install</phase>
<goals>
<goal>install-file</goal>
</goals>
<configuration>
<packaging>jar</packaging>
<artifactId>${project.artifactId}</artifactId>
<groupId>${project.groupId}</groupId>
<version>${project.version}</version>
<file>
${project.build.directory}/${project.artifactId}-${project.version}.jar
</file>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<executions>
<execution>
<phase>deploy</phase>
<goals>
<goal>deploy-file</goal>
</goals>
<configuration>
<packaging>jar</packaging>
<generatePom>true</generatePom>
<url>${project.distributionManagement.repository.url}</url>
<artifactId>${project.artifactId}</artifactId>
<groupId>${project.groupId}</groupId>
<version>${project.version}</version>
<file>${project.build.directory}/${project.artifactId}-${project.version}.jar</file>
</configuration>
</execution>
</executions>
</plugin>
The "maven way" is to split out src/main/java into a separate module, and have the war file depend on that.
If you're absolutely resistant to that approach, you may be able to use a profile to alter the contents of the packaging element. I'm not sure if that's possible though.
Separating them is the right way to go. Forcing maven to produce a war and a jar in the same module is possible but will cause you problems down the road.
You should add the corresponding dependency of the artifact in the dependencies of the pom file.
Ex:
<dependency>
<groupId>org.apache.myfaces.core</groupId>
<artifactId>myfaces-api</artifactId>
<version>1.2.2</version>
<scope>compile</scope>
</dependency>
One way to solve this is to have the module build a jar and then use the assembly plugin to build a war file with the jar in WEB-INF/lib of that war. I would strongly recommend against this. You'd be better off having a jar project and a war project with a parent project building both modules.

Categories

Resources