How to compile resources folder with Maven command - maven

I'm using mvn compile to compile my Maven webapp. This project has a resources folder instead of the java folder created for a .jar project. My problem is that mvn finds no sources, and I haven't find a way in the maven docs to proceed this way. Is there a way, either by mvn command options or by pom.xml modification to make mav aware of the resources folder and compile it?
I know changing the name from resources to java makes the deal, but that's a spureous way to proceed.

To include additional source directories in your project you can use the Build Helper Maven Plugin
So for example the following configuration will add the src/main/resources folder of your project as a source folder.
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>${basedir}/src/main/resource</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

Look at your .classpath file. That should have what folders including src and test are added. You can then add additional resources. I would normally use the IDE to look at the build path and add/exclude resources.

Related

How to reference a properties file on Master and Child POM's

I have a master POM file that defines two profiles: QA and Production. It uses the properties-maven-plugin to set some vars that are later used by the wildfly-maven-plugin to deploy the packages to the web server.
Something like this:
[MASTER POM]
<profile>
<id>qa</id>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0.0</version>
<executions>
<execution>
<phase>initialize</phase>
<goals>
<goal>read-project-properties</goal>
</goals>
<configuration>
<files>
<file>../build-qa.properties</file>
</files>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
And on the Child POMs:
<parent>
<groupId>fhng.apps</groupId>
<artifactId>fhng-build-all</artifactId>
<version>1.0</version>
</parent>
(...)
<plugin>
<groupId>org.wildfly.plugins</groupId>
<artifactId>wildfly-maven-plugin</artifactId>
<version>1.1.0.Alpha11</version>
<executions>
<execution>
<phase>install</phase>
<goals>
<goal>deploy</goal>
</goals>
</execution>
</executions>
<configuration>
<hostname>${wildfly.server}</hostname>
<port>${wildfly.port}</port>
<username>${wildfly.username}</username>
<password>${wildfly.password}</password>
<filename>myapp.war</filename>
</configuration>
</plugin>
the Master POM is located on the project root and each particular web application is located on a sub-folder. This works if I run mvn install from each particular project folder. I would very much like to run "mvn -Pqa clean install" from the master pom folder. However it fails because the master pom references ..\build-qa.properties which works from each project but obviously is invalid from the parent directory.
Is where a way to solve this? Is it possible to reference a file relative to the Master POM folder, irrespective of which particular POM is built? I understand that this approach breaks the maven premise that the parent package must not necessarily be present on our working dir.
As an alternative, is there a way to reference the properties files as an artifact of the parent package? So that maven is able to get said parent package from the repo and use a file inside it?
I would also accept a way to "skip" ou "disable" the initialize/compile/install phase on the parent pom so that it won't try to read the .properties files.
Thank you!
If in root folder you put directory .mvn/, you can refer to root folder by ${maven.multiModuleProjectDirectory}
I current case:
<file>${maven.multiModuleProjectDirectory}/build-qa.properties</file>
Ref: http://takari.io/2015/03/20/mmp.html
PS. Remember that empty folder is not comitted to git

Building a dependencies jar with Maven

Currently our maven build includes all the dependencies in the jar, using jar-with-dependencies.
We want to split this into two separate jars, one with the project application code and files, and one with the dependencies.
How is this done?
Thanks
This is done using the maven Assembly plugin
http://maven.apache.org/plugins/maven-assembly-plugin/
Use the maven-shade-plugin instead with following configuration:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.1</version>
<configuration>
<finalName>${project.artifactId}-${project.version}-libs</finalName>
<artifactSet>
<excludes>
<exclude>${project.groupId}:${project.artifactId}</exclude>
</excludes>
</artifactSet>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
The trick is to define a specific final name. This avoids the replacement of the default jar, which is packaged by the maven-jar-plugin. The default name is ${project.artifactId}-${project.version}. So simply add a suffix like libs. Then exclude the artifact itself, because the classes should not be packaged twice.
The build will result in two jar files:
${project.artifactId}-${project.version}.jar, containing the classes and files of the project
${project.artifactId}-${project.version}-libs.jar, containing the content of all the dependencies

build dependency on shared JavaScript file

Context:
We use a third-party maven plugin to compile lesscss into css
(lesscss-maven-plugin)
We cannot modify or update this plugin.
We cannot switch to another plugin, use grunt, or any other build
tool.
The plugin allows us to specify a JS file as a workaround to
upgrading the plugin itself.
Currently we have to have a copy of the
file included with each and every project
Question:
How can the JS file be packaged and a build/compile time dependency defined in the POM so that the file can be shared across projects easily?
Is it possible to use a URL for the file path instead of the {project.basedir}?
Example POM Snippet:
<plugin>
<groupId>org.lesscss</groupId>
<artifactId>lesscss-maven-plugin</artifactId>
<version>1.3.0</version>
<configuration>
<sourceDirectory>${project.basedir}/src/main/resources/less</sourceDirectory>
<outputDirectory>${project.basedir}/src/main/webapp/css</outputDirectory>
<compress>true</compress>
<includes>
<include>example.less</include>
</includes>
<lessJs>${project.basedir}/src/main/webapp/lib/less-1.7.0.min.js</lessJs>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>

Change default pom.xml and project layout of Maven

I am just starting using maven and I use Apache Maven Shade Plugin a lot. Is it possible to add these code
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.2</version>
<configuration>
<!-- put your configurations here -->
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
to default pom.xml. Yes, it can change setting.xml to make this plugin work with all project. But if I have some specific project which will not need this plugin, do I have to rewrite setting.xml again?
Another problem is that is it possible to change project layout of Maven. I use git a lot. Can I add sample .gitignore every time when I run mvn archetype:generate.
For you first issue, I think you can benefit from the parent POM:
http://books.sonatype.com/mvnex-book/reference/multimodule-sect-simple-parent.html
It's a defined POM file in which you put whatever you want. You publish it as a "pom" in your Maven repository and then, you can inherit from it in other projects. It is very convenient to lock dependency verions as well.
Your second issue seems more related to the archetype you are using than maven itself. You will probably have to create your own with a default .gitignore in it.

How to include external source files in maven-jar-plugin

We are building a jar file from external(to the project) classes.
That works fine but we have not been able to figure out how to also include the external source files. I have tried using the "< includes >" tag but only end up with a manifest file in the final jar when used. I have looked at using the maven-resources-plugin but either I used it wrong or it doesn't work in my case. Here is a copy of our of code:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<!-- <phase>generate-resources</phase> -->
<phase>clean</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classesDirectory>${itendant.path}/web/rocket/WEB-INF/classes</classesDirectory>
<finalName>${itendant.jar.name}</finalName>
<outputDirectory>${itendant.jar.path}</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
Sources? Do you mean external dependencies? These should also be managed with maven, using mvn deploy:deploy-file as described on http://maven.apache.org/plugins/maven-deploy-plugin/usage.html , and imported in your pom.xml.
If you really mean external resources, then a proper resources declaration would be:
<project>
...
<build>
...
<resources>
<resource>
<directory> [your folder here] </directory>
</resource>
</resources>
...
</build>
...
</project>
You can have multiple tags if you have multiple resource directories, of course. Also note that building anything during clean is questionable, as clean is not run every build - package would be a better option.

Resources