Whenever I put FXML files into the /src/main/java catalog, it seems like they aren't being included into the final OSGi jar during compilation. I suppose Maven removes them from there because it thinks that FXML files should only reside in the /src/main/resouces catalog. Is there a way to stop Maven from doing this (i.e. just leave them there)?
EDIT 1
The only solution I have found for now is:
<build>
<plugins>
....
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.7</version>
<executions>
<execution>
<id>copy-resources</id>
<!-- here the phase you need -->
<phase>generate-sources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target/classes/</outputDirectory>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.fxml</include>
<include>**/*.css</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
However, as far I understand, there is another, shorter way of achieving this, i.e. without the use of additional plugins, in the <build>...</build> node. How do I use the shorter method instead?
Yes by default src/main/resources is for resource files.
You can use maven resources plugin to override this behavior.
Here's an example. In your pom.xml
...
<build>
<resources>
<resource>
<targetPath>com/company/projectname</targetPath>
<directory>src/main/java/com/company/projectname</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
where <directory> is the source package which includes resource files(in your case xml files) and <targetPath> is the target where you want the resources to reside. You can omit <targetPath> if you are ok with having these resources in the root path of the jar.
Related
We have a common logback.xml file that we would like to use across different web apps. It includes a RollinFileAppender which should see files named as the project artifactId. The logback.xml includes a property like so
<property name="LOG_FILE_NAME" value="$project.artifactId}"/>
Within our web project we would like to include a dependency e.g. logging-setup
<dependency>
<groupId>our.group.id</groupId>
<artifactId>logging-setup</artifactId>
</dependency>
How do we easily allow the maven-war-plugin to filter this file so that the ${project.artifactId} reference is replaced with the actual project.artifactId? I think it can be done using a combination of the maven-dependency-plugin and the maven-war-plugin something like below. However that would need to be included in every project POM. Is there an easier way?
Thanks,
Paul
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>${maven-dependency-plugin.version}</version>
<executions>
<execution>
<id>unpack</id>
<phase>compile</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>our.group.id</groupId>
<artifactId>logging-setup</artifactId>
<version>${logging-setup.version}</version>
<type>jar</type>
<overWrite>true</overWrite>
<outputDirectory>${basedir}/src/main/resources</outputDirectory>
<includes>**/logback.xml</includes>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
<webResources>
<resource>
<directory>${basedir}/src/main/resources</directory>
<filtering>true</filtering>
<targetPath>WEB-INF/classes</targetPath>
<includes>
<include>logback.xml</include>
</includes>
</resource>
</webResources>
<packagingExcludes>WEB-INF/lib/logging-setup-*.jar</packagingExcludes>
</configuration>
</plugin>
</plugins>
</build>
After this logback.xml file is put inside an artifact jar file, it cannot be changed easily anymore during build time.
I would suggest a slightly different approach:
rename this file to e.g. logback-YOURCOMPANY.xml and change it so it can be included.
create a tiny logback.xml file put in each project as-is which sets the property as you do, and then includes logback-YOURCOMPANY.xml
enable filtering on this tiny logback.xml file so the variable is expanded during Maven build.
See https://logback.qos.ch/manual/configuration.html#fileInclusion for details.
I have a war file with application.properties in default resources. I want to, when deploying on Tomcat, put these properties into an external folder like /webapps/example, not /webapps/namewar.
Can I configure my Maven or do something to achieve that?
Thank you so much.
You can use maven-resources-plugin's copy-resources goal to copy a file from your source tree to some directory. You need to add something like the following to your pom.xml:
<project>
...
<build>
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<id>copy-resources</id>
<!-- you may want to choose another phase -->
<!-- see https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html#Lifecycle_Reference -->
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<!-- outputDirectory can be absolute or relative to ${basedir} -->
<outputDirectory>/webapps/example</outputDirectory>
<resources>
<resource>
<directory>src/main/resources</directory><!-- or whereever your application.properties reside -->
<includes>
<include>application.properties</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
...
</build>
...
</project>
I have a Maven project with a structure:
Project
|---lib
| |---<files and folders I want to include>
|
|---src
| |---<regular files and folders>
|
|---pom.xml
In my pom.xml I have:
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies-third-party</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>target/dist/lib/third-party</outputDirectory>
<excludeGroupIds>...</excludeGroupIds>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
And it copies all my maven dependencies to target/dist/lib/third-party directory. How can I also include all files/folders from lib (see in structure above) folder to that location?
Since these files are configuration and properties files i would classify them as resources and would use the maven-resources-plugin to include them.
<resources>
<!-- The resources in the lib folder -->
<resource>
<directory>lib</directory>
<targetPath>${project.build.directory}/dist/lib/third-party</targetPath>
<!-- add this if you want to define parameters in these resources -->
<filtering>true</filtering>
</resource>
<!-- We need to redeclare the project resources again -->
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
use maven-assembly-plugin
It answers exactly your question
I want to show README.md file like help page in my web application. In order not to create duplicate, I need to copy by mvn from project path into resources.
How can I do so?
Any idea?
I have tried:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.3</version>
<configuration>
<!-- this is important -->
<overwrite>true</overwrite>
<!-- target -->
<outputDirectory>${basedir}/target/classes</outputDirectory>
<resources>
<resource>
<!-- source -->
<directory>/</directory>
<include>
<filter>**/README.md</filter>
</include>
</resource>
</resources>
</configuration>
</plugin>
The simplest solution would be to move the appropriate file(s) to src/main/resources folder whereas the second solution could be like this:
<build>
<resources>
<resource>
<directory>${project.basedir}</directory>
<includes>
<include>README.md</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
No need for maven-resources-plugin to be configured this can be handled by the usual life cylcle and resource handling. The only thing you might need to adapt is the folder where the README.md is located. If you like having filtering you need to add the <filtering>true</filtering> part as well.
Copying something via Maven during the build into src/** is in general a bad idea, cause those folders are controled by version control systems which will result in uncommitted changes which you don't like to have.
Note: It would be wise to check for up-to-date versions of plugins (cause 2.3 is of 2008!). The list of the current plugin versions can be found here: http://maven.apache.org/plugins/
This works for me in one of my projects:
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.4.3</version>
<executions>
<execution>
<id>copy-resources</id>
<phase>validate</phase>
<!-- when to execute copy operation -->
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<resources>
<resource>
<directory>${basedir}/pathTo.MD</directory>
</resource>
</resources>
<overwrite>true</overwrite>
<outputDirectory>${project.build.directory}/${project.build.finalName}/classes</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
I see that i have the phase and the goal extra from your version. I also used variables for the output location.
I would recommend you to be more equal to this example: http://maven.apache.org/plugins/maven-resources-plugin/examples/copy-resources.html
With "more equal" I mean like using <executions> tag.
You can of course leave out things like <id> and filtering.
The following worked fine for me:
<project>
...
<build>
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.7</version>
<executions>
<execution>
<id>myID</id>
<!-- here the phase you need -->
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target/extra-resources</outputDirectory>
<overwrite>true</overwrite>
<resources>
<resource>
<directory>src</directory>
<!-- Details about filtering: http://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html -->
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
...
</build>
...
</project>
I have a maven project which uses wsgen to generate XSD files from the compiled java classes.
The problem is that I want to add the generated files to the jar as resources. But since the resource phase runs before the process-classes phase, I can't add them.
Is there a way to tell maven to add additional resources that were generated at the process-classes phase?
I would suggest to define the output directory for the XSD files into target/classes (may be with a supplemental sub folder which will be packaged later during the package phase into the jar. This can be achieved by using the maven-resources-plugin.
<project>
...
<build>
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
<executions>
<execution>
<id>copy-resources</id>
<phase>process-classes</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.outputDirectory}</outputDirectory>
<resources>
<resource>
<directory>${basedir}/target/xsd-out</directory>
<filtering>false</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
...
</build>
...
</project>
You need to take care that the resources plugin is positioned after the plugin which is used to call the wsgen part. You can also use the prepare-package phase instead to make sure the resources will be correctly packaged.