Cannot import webservice stubs in client - java

I am trying to learn about jax-ws and created a simple Bottom-Up HelloWorld webservice in order to train myself:
import org.jboss.annotation.ejb.LocalBinding;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
#WebService(serviceName = "HelloWorldWebServiceExperiment",
portName = "HelloWorldPort",
name = "HelloWorld")
#SOAPBinding(style = SOAPBinding.Style.DOCUMENT,
use = SOAPBinding.Use.LITERAL,
parameterStyle = SOAPBinding.ParameterStyle.WRAPPED)
#Stateless
#Remote
#LocalBinding(jndiBinding = "training.webservice.HelloWorldWebServiceRemote/local")
public class HelloWorldWebServiceBean {
private static final String HELLO_WORLD = "Hello World";
#WebMethod
public String helloWorld() {
return HELLO_WORLD;
}
}
When I deploy this webservice I can access the wsdl, so that seems to work fine.
Then I tried to generate a webservice client. And to do so I needed to create stubs, which I tried to create with the maven plugin jaxws-maven-plugin. Here is my pom.xml:
<?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>
<groupId>training.webservice</groupId>
<artifactId>HelloWorldWebServiceExperiment</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<wsdlPath>${project.build.directory}</wsdlPath>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3</version>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<webResources>
<resource>
<targetPath>WEB-INF</targetPath>
<directory>src/main/webapp/WEB-INF</directory>
<includes>
<include>web.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</webResources>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxws-maven-plugin</artifactId>
<executions>
<execution>
<id>genwsdl</id>
<phase>process-classes</phase>
<goals>
<goal>wsgen</goal>
</goals>
<configuration>
<genWsdl>true</genWsdl>
<sei>training.webservice.HelloWorldWebServiceBean</sei>
</configuration>
</execution>
<execution>
<id>consumeWsdlForStubs</id>
<phase>process-classes</phase>
<goals>
<goal>wsimport</goal>
</goals>
<configuration>
<wsdlDirectory>target/jaxws/wsgen/wsdl</wsdlDirectory>
<wsdlFiles>
<wsdlFile>HelloWorldWebServiceExperiment.wsdl</wsdlFile>
</wsdlFiles>
<keep>true</keep>
<destDir>target/classes</destDir>
<sourceDestDir>target/stubs</sourceDestDir>
<verbose>true</verbose>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<id>package-wsclient-jars</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classesDirectory>target/stubs</classesDirectory>
<classifier>wsclient</classifier>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<!-- JAX-WS Annotations -->
<dependency>
<groupId>javax.jws</groupId>
<artifactId>jsr181-api</artifactId>
<version>1.0-MR1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jboss</groupId>
<artifactId>jbossws-spi</artifactId>
<version>1.0.0.GA</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jboss</groupId>
<artifactId>jboss-ejb3x</artifactId>
<version>4.2.2</version>
<!--<scope>provided</scope>-->
</dependency>
<dependency>
<groupId>jboss</groupId>
<artifactId>jboss-annotations-ejb3</artifactId>
<version>4.2.2.GA</version>
<!--<scope>provided</scope>-->
</dependency>
<dependency>
<groupId>org.jboss</groupId>
<artifactId>jboss-jaxws</artifactId>
<version>4.2.2</version>
<scope>provided</scope>
</dependency>
</dependencies>
The Build with maven will generate the follwoing output and a .jar file with generated .java files:
> mvn clean install
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building Unnamed - training.webservice:HelloWorldWebServiceExperiment:war:1.0-SNAPSHOT
[INFO] task-segment: [clean, install]
[INFO] ------------------------------------------------------------------------
[INFO] [clean:clean {execution: default-clean}]
[INFO] Deleting directory ---/HelloWorldWebServiceExperiment/target
[INFO] [resources:resources {execution: default-resources}]
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO] [compiler:compile {execution: default-compile}]
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 2 source files to ---/HelloWorldWebServiceExperiment/target/classes
[INFO] [compiler:compile {execution: default}]
[INFO] Nothing to compile - all classes are up to date
[INFO] [jaxws:wsgen {execution: genwsdl}]
[INFO] [jaxws:wsimport {execution: consumeWsdlForStubs}]
[INFO] Processing: ---/HelloWorldWebServiceExperiment/target/jaxws/wsgen/wsdl/HelloWorldWebServiceExperiment.wsdl
[INFO] jaxws:wsimport args: [-s, ---/ /HelloWorldWebServiceExperiment/target/jaxws/wsimport/java, -d, ---/HelloWorldWebServiceExperiment/target/classes, -verbose, -Xnocompile, ---/HelloWorldWebServiceExperiment/target/jaxws/wsgen/wsdl/HelloWorldWebServiceExperiment.wsdl]
parsing WSDL...
generating code...
training/webservice/HelloWorld.java
training/webservice/HelloWorldResponse.java
training/webservice/HelloWorldWebServiceExperiment.java
training/webservice/HelloWorld_Type.java
training/webservice/ObjectFactory.java
training/webservice/package-info.java
[INFO] [resources:testResources {execution: default-testResources}]
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory ---/HelloWorldWebServiceExperiment/src/test/resources
[INFO] [compiler:testCompile {execution: default-testCompile}]
[INFO] Nothing to compile - all classes are up to date
[INFO] [surefire:test {execution: default-test}]
[INFO] No tests to run.
[INFO] [war:war {execution: default-war}]
[INFO] Packaging webapp
[INFO] Assembling webapp[HelloWorldWebServiceExperiment] in [---/HelloWorldWebServiceExperiment/target/HelloWorldWebServiceExperiment-1.0-SNAPSHOT]
[INFO] Processing war project
[INFO] Copying webapp webResources[---/HelloWorldWebServiceExperiment/src/main/webapp/WEB-INF] to[---/HelloWorldWebServiceExperiment/target/HelloWorldWebServiceExperiment-1.0-SNAPSHOT]
[INFO] Copying webapp resources[---/HelloWorldWebServiceExperiment/src/main/webapp]
[INFO] Webapp assembled in[33 msecs]
[INFO] Building war: ---/HelloWorldWebServiceExperiment/target/HelloWorldWebServiceExperiment-1.0-SNAPSHOT.war
[INFO] [jar:jar {execution: package-wsclient-jars}]
[WARNING] JAR will be empty - no content was marked for inclusion!
[INFO] Building jar: ---/HelloWorldWebServiceExperiment/target/HelloWorldWebServiceExperiment-1.0-SNAPSHOT-wsclient.jar
[INFO] [install:install {execution: default-install}]
[INFO] Installing ---/HelloWorldWebServiceExperiment/target/HelloWorldWebServiceExperiment-1.0-SNAPSHOT.war to ~/.m2/repository/training/webservice/HelloWorldWebServiceExperiment/1.0-SNAPSHOT/HelloWorldWebServiceExperiment-1.0-SNAPSHOT.war
[INFO] Installing ---/HelloWorldWebServiceExperiment/target/HelloWorldWebServiceExperiment-1.0-SNAPSHOT-wsclient.jar to ~/.m2/repository/training/webservice/HelloWorldWebServiceExperiment/1.0-SNAPSHOT/HelloWorldWebServiceExperiment-1.0-SNAPSHOT-wsclient.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2 seconds
[INFO] Finished at: Tue Aug 25 09:31:20 CEST 2015
[INFO] Final Memory: 32M/487M
[INFO] ------------------------------------------------------------------------
EDIT (for precision): When I try use the stub archive in another project to create a webclient, (i do this with intellij idea), idea automatically finds the file from my maven repository and suggests it to me, generating the following dependency:
<dependency>
<groupId>training.webservice</groupId>
<artifactId>HelloWorldWebServiceExperiment</artifactId>
<version>1.0-SNAPSHOT-wsclient</version>
</dependency>
however none of the genereated .java files can actually be imported and used in code! E.g. idea fails on usage or import
import training.webservice.HelloWorld;
private training.webservice.HelloWorld helloWorld;
with the error message "Cannot resolve symbol 'HelloWorld'"
What am I doing wrong? Is this connected to the fact that my stub only contains .java files, but no .class files? Obviously the plugin jaxws-maven-plugin runs wsimport with the -xnocompile flag, but I haven't been able to figure out how to configure maven not to do that, none of the options keep, destDir or sourceDestDir have had any effect on that! Or is this completely irrelevant, as the project importing the stubs should be able to compile them itself and the problem is somewhere else? Any help would be greatly appreciated!

I found a temporary work arround:
I generate a client stub .jar file manually. As my company still works with Java 1.5, I had to install metro in order to use wsimport (I chose version 1.6.2) and then I could call:
wsimport target/generated-sources/wsdl/HelloWorldWebServiceExperiment.wsdl -d target/generated-sources/wsdl/ -keep
afterwards I had .java and .class files which I could manually package into a .jar file, then I moved that .jar into the ressource folder of my webservice client project and manually added the dependency into my pom.xml:
<dependency>
<groupId>training.webservice</groupId>
<artifactId>HelloWorldWebServiceExperiment</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/client.jar</systemPath>
</dependency>
I would prefer an automatic build and deploy from maven, and I am now sure that the problem must lie within my maven setup, however I have still no clue how to do it any better. At least I can continue working on the webservice client for now, and revisit the issue at a later point again!

Related

why mvn clean verify is running surefire plugin only

My project structure is:
Project_Name
|-src/main/java
|-src/test/java
|-default package
|-MyIT.java
|-steps
|-MySteps.java
|-pom.xml
MyIT.java:
#RunWith(SerenityRunner.class)
public class MyIT {
#Steps
MySteps mySteps;
#BeforeClass
public static void setUp() {
RestAssured.baseURI="https://restcountries.com/";
System.out.println("1");
}
#Test
#Title("Check \"Republic Of India\"")
public void verify_that_given_string_found_in_the_response() {
mySteps.whenIOpenURLForIndia();
mySteps.thenRepublicOfIndiaFoundInResponse();
}
}
MySteps.java:
public class MySteps {
#Step("When I Open the URL for India")
public void whenIOpenURLForIndia() {
SerenityRest.given().relaxedHTTPSValidation().basePath("v2/name/{country}").pathParam("country", "INDIA").when().get();
}
#Step("Then \"Republic of India\" found in the response")
public void thenRepublicOfIndiaFoundInResponse() {
SerenityRest.lastResponse().then().body("[1].altSpellings",hasItem("Republic of India"));
}
}
Pom.xml:
<?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>
<groupId>org.restcountries</groupId>
<artifactId>Serenity_RestAssured_Assignment1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Serenity_RestAssured_Assignment1</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/net.serenity-bdd/serenity-core -->
<dependency>
<groupId>net.serenity-bdd</groupId>
<artifactId>serenity-core</artifactId>
<version>3.1.15</version>
</dependency>
<!-- https://mvnrepository.com/artifact/net.serenity-bdd/serenity-junit -->
<dependency>
<groupId>net.serenity-bdd</groupId>
<artifactId>serenity-junit</artifactId>
<version>3.1.15</version>
</dependency>
<!-- https://mvnrepository.com/artifact/net.serenity-bdd/serenity-rest-assured -->
<dependency>
<groupId>net.serenity-bdd</groupId>
<artifactId>serenity-rest-assured</artifactId>
<version>3.1.15</version>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.rest-assured/rest-assured -->
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>4.4.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hamcrest/java-hamcrest -->
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>java-hamcrest</artifactId>
<version>2.0.0.0</version>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M4</version>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<version>3.0.0-M4</version>
<configuration>
<skipTests>false</skipTests>
</configuration>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
Now when I run it using:
mvn clean verify
its output is:
[INFO] Scanning for projects...
[INFO]
[INFO] ---------< org.restcountries:Serenity_RestAssured_Assignment1 >---------
[INFO] Building Serenity_RestAssured_Assignment1 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) # Serenity_RestAssured_Assignment1 ---
[INFO] Deleting C:\ eclipse_workspace\Serenity_RestAssured_Assignment1\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) # Serenity_RestAssured_Assignment1 ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory
C:\ eclipse_workspace\Serenity_RestAssured_Assignment1\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) # Serenity_RestAssured_Assignment1 ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) # Serenity_RestAssured_Assignment1 ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory
C:\ eclipse_workspace\Serenity_RestAssured_Assignment1\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) # Serenity_RestAssured_Assignment1 ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 2 source files to
C:\ eclipse_workspace\Serenity_RestAssured_Assignment1\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:3.0.0-M4:test (default-test) # Serenity_RestAssured_Assignment1 ---
[INFO] Tests are skipped.
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) # Serenity_RestAssured_Assignment1 ---
[WARNING] JAR will be empty - no content was marked for inclusion!
[INFO] Building jar:
C:\ eclipse_workspace\Serenity_RestAssured_Assignment1\target\Serenity_RestAssured_Assignment1-0.0.1-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.305 s
[INFO] Finished at: 2022-06-24T04:14:55+05:30
It shows “tests are skipped” and “jar will be empty”.
My question is why mvn verify is not running MyIT.java test despite that naming convention is proper for integration test(maven failsafe plugin). Maven surefire plugin is skipping the tests but maven failsafe plugin should identify the MyIT.java test.
EDIT: mvn clean test-compile failsafe:integration-test is able to run the MyIT.java test successfully but mvn clean test-compile failsafe:verify does not.
The issue is simply because you have defined the maven-failsafe-plugin in pluginManagement only.
The usual way is to define the version in pluginManagement while the binding has to be done in build area.
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>3.0.0-M7</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
I also recommend to upgrade your used plugins which means define newer versions of all used plugin in pluginManagement.
Furthermore you should not define a configuration for maven-surefire-plugin:
<configuration>
<skip>true</skip>
</configuration>

Unable to create java classes using codehaus.mojo jaxb2-maven-plugin

I have a spring based web application in which I am trying to consume a SOAP service. I am using jaxb2-maven-plugin(org.codehaus.mojo) for that. However I see an empty jaxb2 folder which is crated under target and I dont see any java classes in it. I have the wsdl placed correctly under the resources folder itself.
Below is the plugin config created in pom.xml
<build>
<finalName>${project.artifactId}</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
<resource>
<directory>src/main/java/com/xyz/rap/service/impl/wsdl</directory>
<targetPath>wsdl</targetPath>
</resource>
<resource>
<directory>src/main/config</directory>
<targetPath>config</targetPath>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<id>xjc</id>
<goals>
<goal>xjc</goal>
</goals>
</execution>
</executions>
<configuration>
<!-- Package to store the generated file -->
<packageName>com.xxx.gen.retail.abc</packageName>
<!-- Treat the input as WSDL -->
<wsdl>true</wsdl>
<!-- Input is not XML schema -->
<xmlschema>false</xmlschema>
<!-- The location of the WSDL file -->
<schemaDirectory>${project.basedir}/src/main/resources</schemaDirectory>
<!-- The WSDL file that you saved earlier -->
<schemaFiles>gene.wsdl</schemaFiles>
<!-- Don't clear output directory on each run -->
<clearOutputDir>false</clearOutputDir>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<!-- or whatever version you use -->
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
Below is the log when I run "maven install"
[INFO] Building rap-web Maven Webapp 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
**[INFO] --- jaxb2-maven-plugin:1.6:xjc (xjc) # rap-web ---
[INFO] Generating source...
[WARNING] No encoding specified; default platform encoding will be used for generated sources.
[INFO] parsing a schema...
[INFO] compiling a schema...
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) # rap-web ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 15 resources
[INFO] Copying 3 resources to wsdl
[INFO] Copying 1 resource to config
[INFO]
[INFO] --- maven-compiler-plugin:2.5.1:compile (default-compile) # rap-web ---
[WARNING] File encoding has not been set, using platform encoding Cp1252, i.e. build is platform dependent!
[INFO] Compiling 50 source files to C:\Users\xx67047\DSA-GIT-Projects\10.22.17-dsa-rap-services\rap-web\target\classes**
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) # rap-web ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory C:\Users\xx67047\DSA-GIT-Projects\10.22.17-dsa-rap-services\rap-web\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:2.5.1:testCompile (default-testCompile) # rap-web ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) # rap-web ---
It says Parsing and compiling the schema in logs but I dont see any java classes getting created in the logs and I see an empty jaxb2 folder and an other empty generated-sources folder getting created. Please refer below image for that:
jaxb2-maven-plugin will create classes by default into target/generated-sources/jaxb. if you are stil not getting classes into target folder then If you are using Eclipse, you can add this folder into your project build path: right-clicking on project and selecting "Build Path > Use Source Folder.
then you need to run mvn clean install it will clean or delete the target folder and regenerate everything.
Try the version 2.2 of the codehaus. Keep in mind that you will need to change your xml a bit since there is a difference between 1.x and 2.x.
Having said that I would like to tell you that I'm having a similar issue. But it could be caused by something else. I'm using IntelliJ and it's generating empty jaxb folder under the /target/generated-sources/.
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<id>xjc</id>
<goals>
<goal>xjc</goal>
</goals>
</execution>
</executions>
<configuration>
<sources>
<source>/src/main/resources/xsd/</source>
</sources>
<outputDirectory>${project.basedir}/target/generated-sources/jaxb</outputDirectory>
<clearOutputDir>false</clearOutputDir>
</configuration>
</plugin>

Jar not attaching to new executable jar

I have a java/maven project that runs some database stuff perfectly (classpath is working great). I use a proprietary database jar in order to connect to the database. When I go to package up my software into a shaded jar the proprietary jar isn't referenced. When I check the log files I see this error:
Exception: java.lang.ClassNotFoundException: com.sybase.jdbc4.jdbc.SybDriver
I'm trying to put everything into a shaded jar using Maven. Is there something that I'm not doing? Why is the database jar not working in my new shaded jar?
Adding the shade plugin configuration:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>package.class</mainClass>
</transformer>
</transformers>
<shadedArtifactAttached>true</shadedArtifactAttached>
<shadedClassifierName>launcher</shadedClassifierName>
</configuration>
</execution>
</executions>
</plugin>
I have followed the instructions about setting up a file repository since we don't have a corporate repository.
I created a folder at c:\my-repo and put the lone jconn4.jar file inside. I then added this to my pom.xml:
<repositories>
<repository>
<id>my-local-repo</id>
<url>file://${basedir}/my-repo</url>
</repository>
</repositories>
and
<dependency>
<groupId>company</groupId>
<artifactId>jconn4.jar</artifactId>
<version>16.0</version>
</dependency>
Finally, at a command prompt I ran the command:
$ mvn org.apache.maven.plugins:maven-install-plugin:2.3.1:install-file -Dfile=C:\my-repo\jconn4.jar -DgroupId=company -DartifactId=jconn4.jar -Dversion=16.0 -Dpackaging=jar -DlocalRepositoryPath=C:\my-repo
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building My_Project 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-install-plugin:2.3.1:install-file (default-cli) # My_Project ---
[INFO] Installing C:\my-repo\jconn4.jar to C:\my-repo\company\jconn4.jar\16.0\jconn4.jar-16.0.jar
[INFO] Installing C:\Users\jsmith\AppData\Local\Temp\mvninstall7806841451724024532.pom to C:\my-repo\company\jconn4.jar\16.0\jconn4.jar-16.0.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.613 s
[INFO] Finished at: 2016-12-08T10:26:08-05:00
[INFO] Final Memory: 7M/245M
[INFO] ------------------------------------------------------------------------
That's all well and good but I keep getting this error in my pom.xml now "Missing artifact company:jconn4.jar:jar:16.0"
Have I implemented something incorrectly?

How to run java class in JAR file in maven target directory?

I would like to run my Izpack installer after maven build, but I am getting following output after executing "mvn test":
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building RS IzPack installer
[INFO] task-segment: [test]
[INFO] ------------------------------------------------------------------------
[debug] execute contextualize
[INFO] [resources:copy-resources {execution: copy-resources}]
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 109 resources
[INFO] Copying 4 resources
[INFO] Preparing exec:java
[WARNING] Removing: java from forked lifecycle, to prevent recursive invocation.
[debug] execute contextualize
[INFO] [resources:copy-resources {execution: copy-resources}]
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 109 resources
[INFO] Copying 4 resources
[INFO] [exec:java {execution: default}]
[WARNING]
java.lang.ClassNotFoundException: com.izforge.izpack.installer.Installer
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:285)
at java.lang.Thread.run(Thread.java:595)
[INFO] ------------------------------------------------------------------------
[ERROR] BUILD ERROR
[INFO] ------------------------------------------------------------------------
[INFO] An exception occured while executing the Java class. com.izforge.izpack.installer.Installer
Looks like I have to somehow put generated jar file into classpath, any ideas?
Excerpt from my pom.xml :
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<phase>test</phase>
<goals>
<goal>java</goal> <!-- "exec" also possible -->
</goals>
<configuration>
<mainClass>com.izforge.izpack.installer.Installer</mainClass>
<arguments>
<argument>-console</argument>
<!-- <argument>arg1</argument> -->
</arguments>
</configuration>
</execution>
</executions>
</plugin>
Apache Maven 2.2.1 (r801777; 2009-08-06 21:16:01+0200)
Java version: 1.6.0_20
Java home: C:\Java\jdk16\jre
Default locale: en_GB, platform encoding: Cp1252
OS name: "windows xp" version: "5.1" arch: "x86" Family: "windows"
Martin
Have you looked inside of jar ? It can be that maven didn't included needed classes into jar.
I think You should define the classpath for the java command using -classpath. You need to construct a classpath that will contain Your main class com.izforge.izpack.installer.Installer and all it's dependencies. It can be in a jar, or a class folder or multiple jars. See Wikipedia on how to define a classpath for java call.
You can use something like that to define a dependency for that execution:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<phase>test</phase>
<goals>
<goal>java</goal> <!-- "exec" also possible -->
</goals>
<configuration>
<mainClass>com.izforge.izpack.installer.Installer</mainClass>
<arguments>
<argument>-console</argument>
<!-- <argument>arg1</argument> -->
</arguments>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.codehaus.izpack</groupId>
<artifactId>izpack-standalone-compiler</artifactId>
<version>4.3.4</version>
<scope>compile</scope>
</dependency>
</dependencies>
But there is also a Maven plugin for izpack.

Maven: Java classes don't compile after Ant task

My project generates source code using the Rats! parser generator. Rats! doesn't have a Maven plugin that I'm aware of, so I'm trying to build the parser using an Ant Java
task, like so:
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>generate-sources</phase>
<configuration>
<tasks>
<mkdir dir="${project.build.directory}/generated-sources/main/java/" />
<java classpath="lib/xtc.jar" classname="xtc.parser.Rats">
<arg line="-in ${project.build.sourceDirectory}" />
<arg line="-out ${project.build.directory}/generated-sources/main/java/" />
<arg path="${project.build.sourceDirectory}/Dot.rats" />
</java>
</tasks>
<sourceRoot>
${project.build.directory}/generated-sources/main/java
</sourceRoot>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
The details of what Rats! does aren't important: the end result is that
the above generates Dot.java and places it in
target/generated-sources/main/java. It works fine.
The problem is that, with this plugin element in my pom.xml, none of
the Java files in the project get compiled.
I generated a project
using "mvn archetype:create -DgroupId=foo -DartifactId=bar" and added the
file src/main/java/Dot.rats:
module Dot;
public void Dot = "." !_ ;
(This is a grammar that accepts only files with a single dot.)
If I run "mvn compile" without the plugin element, I get:
$ mvn compile
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building bar
[INFO] task-segment: [compile]
[INFO] ------------------------------------------------------------------------
[INFO] [resources:resources]
[INFO] Using default encoding to copy filtered resources.
[INFO] [compiler:compile]
[INFO] Compiling 1 source file to
/home/chris/src/tests/maven/project1/bar/target/classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1 second
[INFO] Finished at: Wed Jul 01 18:57:08 EDT 2009
[INFO] Final Memory: 6M/67M
[INFO] ------------------------------------------------------------------------
Where the one Java file being compiled is src/main/java/foo/App.java, a Java class created by the archetype (i.e., not a generated source file).
If I do "mvn clean" then add the plugin element invoking Rats!, I get:
$ mvn compile
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building bar
[INFO] task-segment: [compile]
[INFO] ------------------------------------------------------------------------
[INFO] [antrun:run {execution: default}]
[INFO] Executing tasks
[mkdir] Created dir:
/home/chris/src/tests/maven/project1/bar/target/generated-sources/main/java
Rats! Parser Generator, v. 1.14.2, (C) 2004-2008 Robert Grimm
Processing /home/chris/src/tests/maven/project1/bar/src/main/java/Dot.rats ...
I.e., Maven is running Rats! (which is not failing, AFAICT) but not compiling any Java classes, not even the pre-existing class App.java. After the run, I have target/generated-sources/main/java/Dot.java but no target/classes.
I've tried other kinds of Ant tasks and they don't interfere with Java
compilation. For example, if I replace the task element above with an
echo task
<tasks>
<mkdir dir="${project.build.directory}/generated-sources/main/java/" />
<echo file="${project.build.directory}/generated-sources/main/java/Dot.java">
public class Dot { }
</echo>
</tasks>
I get
$ mvn compile
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building bar
[INFO] task-segment: [compile]
[INFO] ------------------------------------------------------------------------
[INFO] [antrun:run {execution: default}]
[INFO] Executing tasks
[INFO] Executed tasks
[INFO] Registering compile source root
/home/chris/src/tests/maven/project1/bar/target/generated-sources/main/java
[INFO] [resources:resources]
[INFO] Using default encoding to copy filtered resources.
[INFO] [compiler:compile]
[INFO] Compiling 2 source files to
/home/chris/src/tests/maven/project1/bar/target/classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2 seconds
[INFO] Finished at: Wed Jul 01 19:03:34 EDT 2009
[INFO] Final Memory: 7M/79M
[INFO] ------------------------------------------------------------------------
Obviously there's something I'm not understanding about how Maven
executes the java task. Is there something simple that I'm doing
wrong? Is there an alternative way to accomplish this task that I
should try (perhaps a more "Maven-native" way)?
[UPDATE] Funny story. I tried replacing the Ant task with a Maven plugin, by writing a RatsMojo class that invokes xtc.parser.Rats directly and replacing the plugin element above with
<plugin>
<groupId>edu.nyu.xtc</groupId>
<artifactId>maven-xtc-plugin</artifactId>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>rats</goal>
</goals>
<configuration>
<inputDirectory>${project.build.sourceDirectory}</inputDirectory>
<outputDirectory> ${project.build.directory}/generated-sources/main/java</outputDirectory>
<grammarFile>${project.build.sourceDirectory}/Dot.rats</grammarFile>
</configuration>
</execution>
</executions>
</plugin>
It does the exact same thing: it runs Rats! then terminates without compiling any of the Java files in the project.
The problem seems to be that Rats! calls System.exit() (or similar), terminating the JVM. I would have thought that Ant would wrap this (the java task is copied over from a build.xml and it doesn't terminate the Ant build), but the following works by forcing the Rats! process into a separate JVM:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.1</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<executable>java</executable>
<arguments>
<argument>-classpath</argument>
<argument>${basedir}/lib/xtc.jar</argument>
<argument>xtc.parser.Rats</argument>
<argument>-in</argument>
<argument>${project.build.sourceDirectory}</argument>
<argument>-out</argument>
<argument>${project.build.directory}/generated-sources/main/java/</argument>
<argument>${project.build.sourceDirectory}/Dot.rats</argument>
</arguments>
</configuration>
</plugin>
or, just add fork="true" to the java task in the antrun plugin.
You need to add the new directory to the maven source directories. It can be done using the buildhelper-maven-plugin. Add this to your pom:
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>${project.build.directory}/generated-sources/main/java/</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

Categories