Exclude packages while generating sources jar in Maven - java

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>

Related

How do I add additional source directories to maven to generate javadoc?

I used this in my pom.xml to recognize the second source directory I have in my project:
<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>src/main/java-extra/</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
But generating javadoc fails saying it can't find the classes in the java-extra directory. How can I make maven javadoc see the second directory?
There is a sourcepath parameter for maven-javadoc-plugin. There you provide list of your source folders, separated by : or ;. See https://maven.apache.org/plugins/maven-javadoc-plugin/javadoc-mojo.html#sourcepath
For example:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.3.2</version>
<configuration>
<sourcepath>src/main/java;${defines.dir}</sourcepath>
</configuration>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>

How to read java files from target folder in spring boot

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.

How to get PMD maven plugin to skip generated source code?

So I'm creating a maven plugin using the maven-plugin-plugin. The HelpMojo in maven-plugin-plugin generates a java source file.
Unfortunately, PMD is picking this up and complaining about it. Is there a way to have PMD ignore just a single source file? Thanks!
Maven PMD Configuration:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<executions>
<execution>
<id>pmd-verify</id>
<goals>
<goal>check</goal>
<goal>cpd-check</goal>
</goals>
<configuration>
<printFailingErrors>true</printFailingErrors>
</configuration>
</execution>
</executions>
</plugin>
Generated sources usually end up (with maven) in a subdirectory in target/generated-sources, for the maven-plugin-plugin it's target/generated-sources/plugin.
You can exclude these complete directories with excludeRoots, e.g.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<executions>
<execution>
<id>pmd-verify</id>
<goals>
<goal>check</goal>
<goal>cpd-check</goal>
</goals>
<configuration>
<printFailingErrors>true</printFailingErrors>
<excludeRoots>
<excludeRoot>target/generated-sources/plugin</excludeRoot>
</excludeRoots>
</configuration>
</execution>
</executions>
</plugin>
There is also a file based exclude option.

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.

Maven: How to handle generated sources for test(only)?

Usually generated sources should be created in the target dir. But how do I handle classes that are only used for test? I dont want that these classes get packaged in my jar. Is there a common way to deal with this situation?
Use maven build helper plugin's add-test-source goal to add your generated test source files to the build -> http://mojo.codehaus.org/build-helper-maven-plugin/add-test-source-mojo.html
It ensures that the directories added by this goal will be picked up automatically by the compiler plugin during test-compile phase of the build.
EDIT
Here is the example of how to generate code for testign with cxf-codegen-plugin
<build>
<plugins>
...
<plugin>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-codegen-plugin</artifactId>
<version>${cxf.version}</version>
<executions>
<execution>
<id>generate-test-sources</id>
<phase>generate-test-sources</phase>
<configuration>
<sourceRoot>${project.build.directory}/generated/cxf</sourceRoot>
<wsdlOptions>
<wsdlOption>
<wsdl>${basedir}/src/main/wsdl/myService.wsdl</wsdl>
</wsdlOption>
</wsdlOptions>
</configuration>
<goals>
<goal>wsdl2java</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>${build-helper-maven-plugin.version}</version>
<executions>
<execution>
<id>add-test-sources</id>
<phase>generate-test-sources</phase>
<goals>
<goal>add-test-source</goal>
</goals>
<configuration>
<sources>
<source>${project.build.directory}/generated/cxf</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
...
</plugins>
</build>

Categories