How to read java files from target folder in spring boot - java

I've been trying to read java files that are stored inside target/generated-sources folder. To store these files I have used below plugin in pom.xml file
<!-- For Code Generation -->
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<version>0.12.3</version>
<executions>
<execution>
<id>add-source-for-demoapp</id>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<schemaDirectory>src/main/resources/xsd</schemaDirectory>
<schemaIncludes>
<include>myschema.xsd</include>
</schemaIncludes>
<generateDirectory>target/generated-sources/xjc/workflow</generateDirectory>
<generatePackage>com.websystique.xml.workflow</generatePackage>
<!-- For including equals,hashcode and toString methods in generated code -->
<plugins>
<plugin>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-basics</artifactId>
<version>0.9.4</version>
</plugin>
</plugins>
<args>
<arg>-Xequals</arg>
<arg>-XhashCode</arg>
<arg>-XtoString</arg>
</args>
</configuration>
</execution>
</executions>
</plugin>
Now this plugin generated few java files and to access those file I have used below plguin inside pom.xml
<!-- For Adding Generated code directory as source folder -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.9</version>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>${basedir}/target/generated-sources/xjc/workflow/com.websystique.xml.workflow</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
I have tried several ways to read these files from target folder as shown below. But nothing is working.
Apart from above 2 plugins I'm also using spring-boot-maven-plugin sonar-maven-plugin maven-surefire-plugin

https://www.benchresources.net/jaxb-a-xml-parsing-technique/
Once files are generated then add generated/java/source folder under classpath in eclipse.

Related

Add generated Apache Avro source with build-helper-maven-plugin

I'm using Apache avro to generate some pojos, all work very well in run, expect that the generated source is marked as inexistent in imports on IDE (intellij) .
I tried to use build-helper-maven-plugin to add source, but it doesn't work
this is my maven configuration for apache avro and build helper plugins :
<plugin>
<groupId>org.apache.avro</groupId>
<artifactId>avro-maven-plugin</artifactId>
<version>${avro.version}</version>
<configuration>
<stringType>String</stringType>
</configuration>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>schema</goal>
</goals>
<configuration>
<sourceDirectory>${project.basedir}/src/main/avro/</sourceDirectory>
<outputDirectory>${project.build.directory}/generated-sources/</outputDirectory>
<imports>
<import>${project.basedir}/src/main/avro/errorkind.avsc</import>
</imports>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>${project.build.directory}/generated-sources</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
Try changing your pom with following and run clean install and then you should be able to import.
<configuration>
<sourceDirectory>${project.basedir}/src/main/resources/</sourceDirectory>
<source>${project.build.directory}/generated-sources</source>
</configuration>
In POM InteliJ may give you a fake error after adding this but your build will success.
"generated-sources" reside under ${project.build.directory}/target folder
Also, try marking "generated-sources" as source directory. You can do that by:
Project Structure → Modules → Click the generated-sources folder and make it a sources folder.

How to delete a file while doing maven install (build) from target directory

I am looking to delete a folder (api-docs) from the target folder. When I do maven build, when the target folder is generated, it should exclude that folder(api-docs). Target contains Classes, codegen, generated-sources, javadoc-bundle-options, maven-archiver, maven-status, test-classes and a war file. I need to exclude(api-docs) which is present in codegen Codegen > generated-sources> web> api-docs( contains css, fonts, images, lang, lib, specs and some other js and html files)
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<phase>build</phase>
<goals>
<goal>build</goal>
</goals>
<configuration>
<tasks>
<delete>
<fileset> dir="${project.build.outputDirectory}/target/codegen/generated-sources/web/api-docs"/>
</delete>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
`
I added that in pom.xml, but couldn't able to delete. Please suggest
Here is the entire contents of build from pom file
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>com.xxx.chassis.api.plugins</groupId>
<artifactId>codegen-maven-plugin</artifactId>
<version>${codegen.plugin.version}</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<templateDir>chassis-archetypes</templateDir>
<configFile>${project.parent.basedir}/codegen-config.yaml</configFile>
<specifications>
<specification>${project.parent.basedir}/partnerships-originations-product-offer-id.yaml</specification>
</specifications>
<basePackage>com.xxx.papi.popoi</basePackage>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<phase>build</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<delete>
<fileset dir="${project.build.directory}/codegen/generated-sources/web/api-docs/swagger-ui.min.js"/>
</delete>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
<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>${codegen.generated-sources}/java</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-javadoc-plugin</artifactId>
<configuration>
<sourcepath>${codegen.generated-sources}/java</sourcepath>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
Please share the entire contents of the build->plugins section of your pom file. It should not be necessary to add the maven-antrun-plugin to your pom. The apidocs folder in your target directory is usually produced by the maven-javadoc-plugin which you possibly have present in your pom file. The first way to prevent the creation of the apidocs folder would be to remove that plugin declaration from your pom. An alternative way would be to supply the maven.javadoc.skip argument as per this answer: https://stackoverflow.com/a/9360317/7810853
The goal for the antrun-plugin is run, not build. Changing the pom.xml accordingly should help:
[...]
<goals>
<goal>run</goal>
</goals>
[...]
You also should double check in which phase the api-doc folder is created and you should call the ant-plugin in a later phase, for example package:
[...]
<phase>package</phase>
[...]
As a third point, the Maven variable ${project.build.outputDirectory} usually references the folder target/classes. Please check this answer for more details. Therfore, you could either use
<fileset dir="${project.basedir}/target/codegen/generated-sources/web/api-docs"/>
or - more precise -
<fileset dir="${project.build.directory}/codegen/generated-sources/web/api-docs"/>

Add one additional File as Project Artifact

Im trying to make a maven project which has only one xml file as artifact.
Currently i zip the file with this Configuration:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>create-distribution</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptors>
<descriptor>assembly/master.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
But i would like to have the file itself be the artifact.
Is it possible to tell the assemply plugin to just use a file as artifact?
You can use the deploy:deploy-file goal instead. It allows you to upload arbitrary files.
You can use the build-helper-maven-plugin and the goal attach-artifact which looks like this.
<project>
...
<build>
<plugins>
<plugin>
<!-- add configuration for antrun or another plugin here -->
</plugin>
...
<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>some file</file>
<type>extension of your file </type>
<classifier>optional</classifier>
</artifact>
...
</artifacts>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
But usually I can't recommend that.

How to generate Java classes from WSDL with jaxb2-maven-plugin 2.x?

I am trying using the pluggin jaxb2-maven-plugin to create the Java class from the wsdl.
With the version 1.5 this code from Generate classes with jaxb2-maven-plugin from WSDL works:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<id>xjc</id>
<goals><goal>xjc</goal></goals>
</execution>
</executions>
<configuration>
<!-- Package to store the generated file -->
<packageName>com.example.demo.wsdl</packageName>
<!-- Treat the input as WSDL -->
<wsdl>true</wsdl>
<!-- Input is not XML schema -->
<xmlschema>false</xmlschema>
<!-- The WSDL file that you saved earlier -->
<schemaFiles>horarios.wsdl</schemaFiles>
<!-- The location of the WSDL file -->
<schemaDirectory>${project.basedir}/src/main/resources</schemaDirectory>
<!-- The output directory to store the generated Java files -->
<outputDirectory>${project.basedir}/src/main/java</outputDirectory>
<!-- Don't clear output directory on each run -->
<clearOutputDir>false</clearOutputDir>
</configuration>
</plugin>
But when use plugin version 2.3.1, I get this error:
Failed to execute goal org.codehaus.mojo:jaxb2-maven-plugin:2.3.1:xjc (xjc) on project demo: MojoExecutionException: NoSchemasException -> [Help 1]
Does someone know how use WSDL file with this new plugin version?
I have already found the solution.
When the jaxb2-maven-plugin version is >= 2.0 you have to use the follow configuration:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<version>2.3.1</version>
<executions>
<execution>
<id>xjc</id>
<goals>
<goal>xjc</goal>
</goals>
</execution>
</executions>
<configuration>
<packageName>com.example.demo.wsdl</packageName>
<sourceType>wsdl</sourceType>
<sources>
<source>src/main/resources/horarios.wsdl</source>
</sources>
<outputDirectory>target/generated-sources/</outputDirectory>
<clearOutputDir>false</clearOutputDir>
</configuration>
</plugin>
The difference is not only the syntax. That version does not create the class within the project (src/main/java), it creates in the directory that you wrote in outputDirectory and in the package of packageName.
When you use the class generated it is transparent like if it would be in the same project.
If you want to begin with a XSD:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<version>2.3.1</version>
<executions>
<execution>
<id>xjc</id>
<goals>
<goal>xjc</goal>
</goals>
</execution>
</executions>
<configuration>
<xjbSources>
<xjbSource>src/main/resources/global.xjb</xjbSource>
</xjbSources>
<sources>
<source>src/main/resources/Ventas.xsd</source>
</sources>
<outputDirectory>${basedir}/src/main/java</outputDirectory>
<clearOutputDir>false</clearOutputDir>
</configuration>
</plugin>

Exclude packages while generating sources jar in Maven

I would like to generate a sources jar but without some packages in my project.
Now I have this in my pom.xml
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<executions>
<execution>
<id>sources</id>
<goals>
<goal>jar</goal>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
How to exclude specific packages?
similar but with netbeans environment
You can configure the maven-source-plugin with the excludes attribute:
List of files to exclude. Specified as fileset patterns which are relative to the input directory whose contents is being packaged into the JAR.
A sample configuration where you would exclude every class under the package com.my.package would be the following:
<plugin>
<artifactId>maven-source-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>sources</id>
<goals>
<goal>jar</goal>
<goal>test-jar</goal>
</goals>
<phase>package</phase>
<configuration>
<excludes>
<exclude>com/my/package/**</exclude>
</excludes>
</configuration>
</execution>
</executions>
</plugin>

Categories