How to include uncompiled java files in a JAR built by Tycho? - maven

I have a source root with *.java files, but I don't want them to be compiled. Instead, the *.java files should be copied into the jar as they are. The use case for this is that the *.java files are templates and hence should be preserved as they are.
To achieve this, I tried to exclude a source folder from compiling phase of my build and I am quite confused by the official documentation to the Tycho OSGi Compiler Plugin. It says I can use parameter excludeResources but I don't really know how to handle all these parameter types. pom.xml is a structured text file and not a source file, that's why I don't understand how to use java.util.Set for that parameter.
My POM goes like this:
...
<build>
<plugins>
...
<plugin>
<groupId>org.eclipse.tycho</groupId>
<artifactId>tycho-compiler-plugin</artifactId>
<version>0.21.0</extensions>
<configuration>
<excludeResources>
<!-- Set of folders consisting of a source folder named "res"
which should be excluded completely from compilation -->
</excludeResources>
</configuration>
</plugin>
</plugins>
</build>
Is it a right approach? If yes, how would I complete the configuration?

Try something like this to remove any occurences of "res" folder and files:
<plugin>
<groupId>org.eclipse.tycho</groupId>
<artifactId>tycho-compiler-plugin</artifactId>
<version>${tycho-version}</version>
<configuration>
<excludeResources>
<excludeResource>**/res</excludeResource>
</excludeResources>
</configuration>
</plugin>
or this, alternatively, for example, to include the "res" folder and exclude .jar files:
<plugin>
<groupId>org.eclipse.tycho</groupId>
<artifactId>tycho-compiler-plugin</artifactId>
<version>${tycho-version}</version>
<configuration>
<includes>
<include>res</include>
</includes>
<excludes>
<exclude>**/*.jar</exclude>
</excludes>
</configuration>
</plugin>
Or, to exclude everything that is not the "res" folder:
<plugin>
<groupId>org.eclipse.tycho</groupId>
<artifactId>tycho-compiler-plugin</artifactId>
<version>${tycho-version}</version>
<configuration>
<excludeResources>
<excludeResource>!**/res</excludeResource>
</excludeResources>
</configuration>
</plugin>

If you want to exclude a whole source root folder from compilation, simply do not add it to any of the src.* entries in build.properties, see [1]
If on the other hand you want to include the *.java files in the source root folder in the resulting jar, add the root folder to the list of bin.includes in build.properties.
excludeResources is unrelated to your problem, as the docs you linked say:
"A list of exclusion filters for non-java resource files which should not be copied to the output directory."
[1] http://eclipse.org/tycho/sitedocs/BuildProperties.html

Related

How to place output Jar and War files in another specified folder(it may be outside of the project)?

i want to place output jar or war in another separate folder which should contain only jar or war file. i.e folder may be outside of the project. is that possible?
Although generally it's not a good idea to deviate from Maven conventions, you can use outputDirectory parameter in maven-jar-plugin to specify a directory other than ${project.build.directory}
<project>
...
<build>
<plugins>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<outputDirectory>path/to/your/folder</outputDirectory>
</configuration>
</plugin>
...
</plugins>
</build>
...
</project>

ant to maven conversion: correct approach?

I am tasked with converting a java project which is created with ant to maven.
This is how the project is set up.
All the sources are stored in src directory.
ant's compiling target is to compile the entire src directory.
ant's packaging target has several sub-targets.
Each target has different jars which has include or exclude directories.
This is the approach that I took.
Find out all dependencies. Store them in DependencyManagement section of parent pom
Create a module and copy entire src directory.
compiled it.
Tried to create separate modules for different jar files.
Problem: the files are in-separable. Most of the files are depending on other files. I tried separating them. It results in creating cyclic dependencies. Hence, this step failed.
Use different profiles and maven-jar-plugin to include or exclude packages.
Question 1 when I tried this mvn install -P profile1,profile2, target has only jar file for profile2. They both have maven-jar-plugin and each has different finalName.
<profile>
<id>profile1</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<includes>
<include>**/src/.../profile1/**</include>
<finalName>profile1-lib</finalName>
</configuration>
</plugin>
</plugins>
</build>
</profile>
Is there a different command or am I doing something wrong?
Question 2 one of the lib has several image files in it.
The above approach does not copy the image files in the result jar.
I understand maven wants all resources in resources directory. I will move the images, but for now I am trying to include them in the jar.
I added maven-resources-plugin.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target/classes</outputDirectory>
<resource>
<directory>src/main/java/jpl/mipl/mdms/FileService/komodo/ui/savannah/subscription/util/resources</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
When I run mvn install -P profile1, I can see the logs saying Copying # resources.
I can also see them being copied to target/classes. the path that I gave to the plug-in. But they are all copied to that directory.
I want to retain the structure, and the result jar still doesn't have it.
Answer: I have to move them to resources directory. maven is strict.
Anything that I should be doing differently?
Extra Question Am I using the correct plug-ins? Or is there more efficient plug-ins that I should be using?
Thank you.
Answer to Question1 : You have missed the </includes> tag.(Probably because of the stackoverflow formatting!). I am not sure whether you can execute a maven command on two profiles at a time. when you do so, only the second profile gets executed. Try executing the command on each profile separately.
Answer to Question2: You have missed the <resources> opening tag.(Probably because of the stackoverflow formatting!). Moreover, if you want to retain the structure, you can mention the structure too in the <outputDirectory> tag, something like the below:
<outputDirectory>${basedir}/target/classes/src/main/java/jpl/mipl/mdms/FileService/komodo/ui/savannah/subscription/util/resources</outputDirectory>
This may look insane!, but may work if you have few resources directory. If you have more resources, then this may become cumbersome. But anyways, check whether this can be helpful!

I don't want to include external jar in my maven war

I don't want to include external jars when I build a war.What should I do?
Though not sure about why are you thinking to exclude the jar files, but yes you can do that if you are using maven-war-plugin
This will work out
<build>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<packagingExcludes>WEB-INF/lib/*.jar</packagingExcludes>
</configuration>
</plugin>
</plugins>
</build>
This a whole bunch of configuration plugin that excludes all .jar files.
This source explains about that and even regex patterns that can be used.

maven-jar-plugin, include upper dir

Build part of POM
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>2.2</version>
<configuration>
<classesDirectory>./</classesDirectory>
<includes>
<include>*.wsdl</include>
<include>*.xsd</include>
<include>sources/</include>
<include>../configuration.doc</include>
</includes>
</configuration>
</plugin>
In target jar I have all wsdl from root dir, xsds from root dir and sources dir.
But no configuration.doc file in jar.
Any ideas?
You should move the configuration.doc into the appropriate directory like src/main/resources.
How about adding the relevant folder from where you want the .doc to be picked up to your project pom using maven resources plugin - specifically the <resources> configuration - there are examples here. This will make the contents available to the jar plugin.

In Maven how to exclude resources from the generated jar?

When I create an executable jar with dependencies (using this guide), all properties files are packaged into that jar too. How to stop it from happening? Thanks.
UPDATE: I tried to exclude them using the Maven resources plugin, but then my application won't find the properties files when I run it in Eclipse (right click on the module -> Run As -> Java Application)
UPDATE: Thanks for your useful answers. I think I'd better spend time to learn Maven, for now I just choose the simplest solution.
To exclude any file from a jar / target directory you can use the <excludes> tag in your pom.xml file.
In the next example, all files with .properties extension will not be included:
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>*.properties</exclude>
</excludes>
<filtering>false</filtering>
</resource>
</resources>
</build>
By convention, the directory src/main/resources contains the resources that will be used by the application. So Maven will include them in the final JAR.
Thus in your application, you will access them using the getResourceAsStream() method, as the resources are loaded in the classpath.
If you need to have them outside your application, do not store them in src/main/resources as they will be bundled by Maven. Of course, you can exclude them (using the link given by chkal) but it is better to create another directory (for example src/main/external-resources) in order to keep the conventions regarding the src/main/resources directory.
In the latter case, you will have to deliver the resources independently as your JAR file (this can be achieved by using the Assembly plugin). If you need to access them in your Eclipse environment, go to the Properties of your project, then in Java Build Path in Sources tab, add the folder (for example src/main/external-resources). Eclipse will then add this directory in the classpath.
This calls exactly for the using the Maven JAR Plugin
For example, if you want to exclude everything under src/test/resources/ from the final jar, put this:
<build>
<plugins>
<!-- configure JAR build -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.1</version>
<configuration>
<excludes>
<exclude>src/test/resources/**</exclude>
</excludes>
</configuration>
</plugin>
...
Files under src/test/resources/ will still be available on class-path, they just won't be in resulting JAR.
Put those properties files in src/test/resources. Files in src/test/resources are available within Eclipse automatically via eclipse:eclipse but will not be included in the packaged JAR by Maven.
Exclude specific pattern of file during creation of maven jar using maven-jar-plugin.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3</version>
<configuration>
<excludes>
<exclude>**/*.properties</exclude>
<exclude>**/*.xml</exclude>
<exclude>**/*.exe</exclude>
<exclude>**/*.java</exclude>
<exclude>**/*.xls</exclude>
</excludes>
</configuration>
</plugin>
Do you mean to property files located in src/main/resources? Then you should exclude them using the maven-resource-plugin. See the following page for details:
http://maven.apache.org/plugins/maven-resources-plugin/examples/include-exclude.html
Another possibility is to use the Maven Shade Plugin, e.g. to exclude a logging properties file used only locally in your IDE:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>${maven-shade-plugin-version}</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>log4j2.xml</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
This will however exclude the files from every artifact, so it might not be feasible in every situation.
When I create an executable jar with dependencies (using this guide), all properties files are packaged into that jar too. How to stop it from happening? Thanks.
Properties files from where? Your main jar? Dependencies?
In the former case, putting resources under src/test/resources as suggested is probably the most straight forward and simplest option.
In the later case, you'll have to create a custom assembly descriptor with special excludes/exclude in the unpackOptions.
here is another solution to exclude all files in resources folder, the final configuration looks like:
<build>
<!-- exclude all files in resources-->
<resources>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>**/**</exclude>
</excludes>
<filtering>false</filtering>
</resource>
</resources>
<!-- other configurations/plugins in the pom.xml-->
</build>
or we can use includes to only package some file or folder.
But this method has a side effect. IDE will also exclude the resource files in target/classes folder. maven-jar-plugin only affect jar file.
I found a better solution to execludes resourses folder using maven-jar-plugin, here we use includes:
<build>
<plugins>
<!-- configure JAR build -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.1</version>
<configuration>
<includes>
<include>**/*.class</include>
</includes>
</configuration>
</plugin>
...

Resources