Axis wsdl2java maven plugin with xmlbeans doesn't include resources folder - maven

I'm using axis2 to re-generate the client code for an updated webservice that I need to use, for a legacy application.
Being a legacy application I would like to avoid changing the code that has been already written, and re-generate the classes as they were generated who-know-how-many years ago by the eclipse plugin, but this time using maven instead of eclipse.
So I seen that they were generated using axis2 and xmlbeans, and I produced the configuration in the maven plugin:
<plugin>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2-wsdl2code-maven-plugin</artifactId>
<version>1.5.6</version>
<executions>
<execution>
<id>TheirsWs</id>
<goals>
<goal>wsdl2code</goal>
</goals>
<phase>generate-sources</phase>
<configuration>
<packageName>it.theirs.ws</packageName>
<wsdlFile>${basedir}/src/main/resources/theirWs.wsdl</wsdlFile>
<generateServerSide>false</generateServerSide>
<databindingName>xmlbeans</databindingName>
<unpackClasses>true</unpackClasses>
</configuration>
</execution>
</executions>
</plugin>
What happen now is a very nice thing.
The plugin generate a .class file in the generated-sources / axis2 / wsdl2code / resource folder, However it is not added by maven to the final package, causing a ClassNotFoundException when calling the webservice.

I solve the problem by adding the resource folder into the JAR using the maven default feature of including and excluding folder. The solution for your case will be:
<build>
<!-- This will the MAVEN to copy the entire folder, you can copy only the .class files -->
<resources>
<resource>
<directory>${project.build.directory}/generated-src/resources</directory>
<includes>
<include>**/*</include>
</includes>
</resource>
</resources>
<plugin>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2-wsdl2code-maven-plugin</artifactId>
<version>1.5.6</version>
<executions>
<execution>
<id>TheirsWs</id>
<goals>
<goal>wsdl2code</goal>
</goals>
<phase>generate-sources</phase>
<configuration>
<packageName>it.theirs.ws</packageName>
<wsdlFile>${basedir}/src/main/resources/theirWs.wsdl</wsdlFile>
<generateServerSide>false</generateServerSide>
<databindingName>xmlbeans</databindingName>
<!-- I add this line just to be easy to referenciate the souce -->
<outputDirectory>${project.build.directory}/generated-src</outputDirectory>
<unpackClasses>true</unpackClasses>
</configuration>
</execution>
</executions>
</plugin>

Had the same problem, i changed the Ant build.xml buildfile from:
<target depends="pre.compile.test" name="compile.src" if="jars.ok">
<javac debug="on" memoryMaximumSize="256m" memoryInitialSize="256m" fork="true" destdir="${classes}" srcdir="${src}">
<classpath refid="axis2.class.path"/>
</javac>
</target>
To:
<target depends="pre.compile.test" name="compile.src" if="jars.ok">
<javac debug="on" memoryMaximumSize="256m" memoryInitialSize="256m" fork="true" destdir="${classes}" srcdir="${src}">
<classpath refid="axis2.class.path"/>
</javac>
<copy todir="${classes}">
<fileset dir="${resources}"/>
</copy>
</target>
The new Copy task add all the resources to the target classes folder so the generated Jar will include them.
Hope it helps.

Related

minify-maven-plugin (com.samaxes.maven v1.7.6) is not replacing the minified files with the original files

I want to minify JS and CSS files in maven project. I have used minify-maven-plugin (com.samaxes.maven v1.7.6). As per the documentation (https://samaxes.github.io/minify-maven-plugin/minify-mojo.html), I have set <nosuffix> and <skipMerge> as true because I want to maintain the file structure and replace the minified files with original files. I have also set the <phase>package</phase>.
After generating and deploying the WAR file, the JS and CSS files are not minified, they stay the same as before.
I also referred to some stackoverflow answers and set the <warSourceExcludes> option as per the suggestion provided at https://stackoverflow.com/questions/22117824/using-samaxes-minify-nosuffix-to-overwrite-original-files
After using the <warSourceExcludes> option, when I deploy the WAR file on the server, the JS and CSS files are not available and the application is showing 404 errors for the same. Please refer to my pom.xml configuration:
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.1</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
<warSourceExcludes>**/*.css,**/*.js</warSourceExcludes>
</configuration>
</plugin>
<plugin>
<groupId>com.samaxes.maven</groupId>
<artifactId>minify-maven-plugin</artifactId>
<version>1.7.6</version>
<executions>
<execution>
<id>default-minify</id>
<phase>package</phase>
<configuration>
<charset>UTF-8</charset>
<webappSourceDir>${project.basedir}/WebContent</webappSourceDir>
<cssSourceDir>./</cssSourceDir>
<cssSourceIncludes>
<cssSourceInclude>**/*.css</cssSourceInclude>
</cssSourceIncludes>
<cssSourceExcludes>
<cssSourceExclude>**/*.min.css</cssSourceExclude>
</cssSourceExcludes>
<jsSourceDir>./</jsSourceDir>
<jsSourceIncludes>
<jsSourceInclude>**/*.js</jsSourceInclude>
</jsSourceIncludes>
<jsSourceExcludes>
<jsSourceExclude>**/*.min.js</jsSourceExclude>
</jsSourceExcludes>
<jsEngine>CLOSURE</jsEngine>
<nosuffix>true</nosuffix>
<skipMerge>true</skipMerge>
</configuration>
<goals>
<goal>minify</goal>
</goals>
</execution>
</executions>
</plugin>
Please suggest a proper solution. Thanks in advance !
The answer given by #Kristof Neirynck for the question How to get maven to build a war with minified files using yuicompressor-maven-plugin works for the issue mentioned.
I changed the <phase> property as prepare-package and added
<webappTargetDir>${project.build.directory}/minify</webappTargetDir>
in minify-maven-plugin after <webappSourceDir> and in the maven-war-plugin I set the following configuration:
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.1</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
<webResources>
<resource>
<directory>${project.build.directory}/minify</directory>
</resource>
</webResources>
</configuration>
</plugin>
After using this configuration, when I deploy the WAR file in the Tomcat the original files are replaced with the minified files.

How do I add a file to the artifact (jar or war) created by maven?

I have an antrun task (goal run on phase prepare-package) set up that creates a file and saves it in /target/foo.bar. How do I add that to the artifact that gets created by maven (depending on module, it could be a jar or a war file)?
I have tried it with resources, with the builder-helper plugin, and the jar plugin - no luck:
<resources>
<resource>
<directory>target</directory>
<includes>
<include>**/foo.bar</include>
</includes>
</resource>
</resources>
That doesn't seem to do anything.
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>attach-artifacts</id>
<phase>package</phase>
<goals>
<goal>attach-artifact</goal>
</goals>
<configuration>
<artifacts>
<artifact>
<file>${project.build.directory}/foo.bar</file>
<type>bar</type>
</artifact>
</artifacts>
</configuration>
</execution>
</executions>
</plugin>
According to debug output, this seems to install something extra in the repo, but doesn't add foo.bar to the artefact.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<includes>
<include>../foo.bar</include>
</includes>
</configuration>
</plugin>
No observable effect, either. :(
(I assume this might work for the war file, but I'd prefer one way to do it for both artifact types, if possible - plus I really need this to work for the jars, too...)
Is there a way to do what I want to do?
(I'd prefer not saving my file in src/main/resources first; I thought the packaging processes would pick files up from the build directory after they were placed there anyway, but I suppose I misunderstood something...)
I had my file saved to project.build.directory. Changing that to project.build.outputDirectory means the jar plugin is picking it up, without the need of any other plugins. Unfortunately, that doesn't sort the war issue out... :(
I had a similar problem and stored the resulting artifact in
target/${project.name}-${project.version}/foo.war
To be more precise, I created the war from a directory using the ant target
<war warfile="target/${project.name}-${project.version}/foo.war" basedir="somedir" />
Then it was correctly added to the surrounding ear.

Ant - Zip Multiple Folders

I'm trying to run an Ant job within Maven to look at a folder, and based on how many folders are in that folder, to create x number of zip files with the name of the folder. I have it working manually, but it would be nice if I didn't have to edit the pom each time I added a new folder to this structure.
<configuration>
<target name="zip">
<zip destfile="root/sub1/sub1.jar">
<zipfileset dir="root/sub1/unpacked/" includes="**" ></zipfileset>
</zip>
</target>
</configuration>
If I were to add sub2 to the root dir, I would like it to be picked up automagically, and create a sub2.jar file (yes, I'm aware I'm using .jar, but the program that is taking these files expects .jar files, but they're not jar files in that they contain any Java code, they're just zip files with jar extensions)
I've tried this
Thanks I had a look at the first link, but perhaps I'm doing it wrong
<target name="checkDir">
<foreach target="zip" param="theFile">
<dirset dir="root" casesensitive="yes">
<include name="**"/>
</dirset>
</foreach>
</target>
<target name="zip">
<!-- <zip destfile="root/${theFile}/${theFile}.jar">
<zipfileset dir="root/${theFile}/unpacked/" includes="**" ></zipfileset>
</zip> -->
<echo message="${theFile}"/>
</target>
I just get
[INFO] --- maven-antrun-plugin:1.7:run (process-javascript-plugin) # war---
[INFO] Executing tasks
zip:
[echo] ${theFile}
[INFO] Executed tasks
Still doesn't seem to be working.
pom.xml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>process-cartridges</id>
<phase>compile</phase>
<configuration>
<target>
<ant antfile="root/build-main.xml"/>
</target>
</configuration>
</execution>
</executions>
</plugin>
build-main.xml
<?xml version="1.0" encoding="UTF-8" ?>
<project>
<target name="checkDir">
<foreach target="zip" param="theFile">
<dirset dir="root" casesensitive="yes">
<include name="**"/>
</dirset>
</foreach>
</target>
<target name="zip">
<zip destfile="root/${theFile}/${theFile}.jar">
<zipfileset dir="root/${theFile}/unpacked/" includes="**" />
</zip>
<echo message="TESTZIP ${theFile}"/>
</target>
</project>
Doesn't seem to be working. Am I missing anything?
You could use the foreach task of Ant to do this, combining the answers from those two posts: Ant: How to execute a command for each file in directory? and Calling foreach in maven-antrun-plugin.
However, you can also have a Maven solution using the iterator-maven-plugin to iterate over all sub-folders and then use the maven-jar-plugin to make the jar:
<plugin>
<groupId>com.soebes.maven.plugins</groupId>
<artifactId>iterator-maven-plugin</artifactId>
<version>0.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>iterator</goal>
</goals>
<configuration>
<folder>root/</folder>
<pluginExecutors>
<pluginExecutor>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
</plugin>
<goal>jar</goal>
<configuration>
<classesDirectory>root/#item#/unpacked</classesDirectory>
<outputDirectory>root/#item#</outputDirectory>
<finalName>#item#.jar</finalName>
</configuration>
</pluginExecutor>
</pluginExecutors>
</configuration>
</execution>
</executions>
</plugin>
This is bound to the package phase. It will iterate over all direct subfolders of the root folder and invoke the jar plugin with the given configuration. #item# is used to refer to the current directory name.

How to precompile a file after filtering?

I am building a .war file which contains a .jsp file. In the .jsp, there is some maven property variables, thus I need to filter it by using maven war plugin.
Besides that, I would like to precompile it, using mojo jspc plugin, and pack the .class file into the war file.
However, the filtered file is not compiled. The result war file consists the filtered .jsp file and the non filtered .class file.
How should I configure the pom.xml in order to compile the filtered .jsp?
My pom file is shown below:
<build>
<finalName>${Component}</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<archiveClasses>true</archiveClasses>
<attachClasses>true</attachClasses>
<packagingExcludes>**/*.vpp,WEB-INF/web.xml,**/eclipselink.jar,**/coherence.jar,** /toplink-grid.jar</packagingExcludes>
<outputFileNameMapping>#{artifactId}#.#{extension}#</outputFileNameMapping>
<webResources>
<resource>
<directory>src/main/webapp/verification</directory>
<targetPath>verification</targetPath>
<filtering>true</filtering>
</resource>
</webResources>
<executions>
<execution>
<phase>process-resources</phase>
</execution>
</executions>
</configuration>
</plugin>
<!-- start jspc -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jspc-maven-plugin</artifactId>
<executions>
<execution>
<id>jspc</id>
<goals>
<goal>compile</goal>
</goals>
<phase>compile</phase>
</execution>
</executions>
</plugin>
<!-- end jspc -->
</plugins>
</build>
The JSP file is stored in src/main/webapp/verification
It is very probable that the plugins execute in the wrong order. Most plugins allow to change the phase they bind to, by adding e.g. <phase>generate-resources</phase>. You need to explicitly bind the plugins to phases in such way to execute the filtering first. Phases are documented here. Choose the appropriate ones to bind to.
Btw, filtering JSPs directly is a pretty bad practice. Instead, generate a .properties file (filter it, or generate dynamically) and load it to Servlet context, so it's available to all JSPs. If you using Spring, this is a breeze. Example below (runtime.properties is the filtered/generated file):
<bean class="org.springframework.web.context.support.ServletContextAttributeExporter">
<property name="attributes">
<map>
<entry key="config">
<util:properties location="classpath:runtime.properties"/>
</entry>
</map>
</property>
</bean>
You then access it in JSPs as config['someKey']. This also eliminated the need to worry about the phase JSPs get filtered.

Full search and replace of strings in source files when copying resources

I have some java source files that use a package prefix (they are emulating some JDK classes). I use these files with the prefix to run against some unit tests. If the tests pass I want to produce a jar that contains the source files but with the package prefix removed from all the java files.
I am using maven for builds. Does any one know of a way to do this? Essentially what I want is something like the resources plugin filtering feature, but that does proper search and replace (like: s/my.package.prefix.//g), rather than filtering on ${vars}.
You can also use
http://code.google.com/p/maven-replacer-plugin/
100% maven and doing exactly what you want and more
This can be solved with the antrun plugin. Firstly the sources need to be copied to the target directory, with:
<build>
...
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.java</include>
</includes>
</resource>
</resources>
...
</build>
Secondly you use the replace task of the antrun plugin to replace the files using the prepare package phase
<build>
...
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>prepare-package</phase>
<configuration>
<tasks>
<replace token= "my.package.prefix." value="" dir="target/classes">
<include name="**/*.java"/>
</replace>
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
...
</build>
This will copy the source files to target/classes in the process-resources phase, do a search and replace on the files inplace in the target/classes directory in the prepare-package phase and finally they will jarred up in the package phase.

Resources