I have a spring boot application that serves vue static files as well. In the pom.xml I use maven-resources-plugin to copy the static files to resources folder. It works as intended the first time, but for some reason it does not overwrite the output directory when I change files the vue project and build the project again, even when I use <overwrite>true</overwrite>. When I delete the output dir manual and build again - it works fine.
My pom.xml:
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id> Copy vue resources </id>
<phase> generate-resources </phase>
<goals>
<goal> copy-resources </goal>
</goals>
<configuration>
<overwrite> true </overwrite>
<outputDirectory> src/main/resources/public </outputDirecroty>
<resources>
<resource>
<directory> ${project.basedir}/../c4i-vue/target/dist </directory>
<includes>
<include>static/</include>
<include>index.html</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
I also tried to put the <overwrite>true</overwrite> in the outside configuration but it didn’t work as well:
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<overwrite> true </overwrite>
</configuration>
<executions>
<execution>
<id> Copy vue resources </id>
<phase> generate-resources </phase>
<goals>
<goal> copy-resources </goal>
</goals>
<configuration>
<outputDirectory> src/main/resources/public </outputDirecroty>
<resources>
<resource>
<directory> ${project.basedir}/../c4i-vue/target/dist </directory>
<includes>
<include>static/</include>
<include>index.html</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
I would love some help, thank you!
It should be working fine..
Maybe the files were updated but the folders still have the last update date?
Related
I have these folders in my project structure
/src/main/java
/src/main/resources
In the "resources" folder I have 2 files: "config.properties" and "logging.properties".
And I have the following "" in my pom.xml:
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>myapp.Main</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
When I run "mvn clean package", it does generate the "target" folder with the jar and the "classes" folder containg the properties file as mentioned above.
To read one of the properties files (after clicking on a Button), I'm using the following code:
Properties prop = new Properties();
prop.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("logging.properties"));
String logFolder = prop.getProperty("myApp.property");
//Do something with 'logFolder'
It runs OK.
But if I change the "myApp.property" in "logging.properties" file, the change doesn't affect the "logFolder" value.
What must I do to be able to dinamically change the property value and make my application read the new value WITHOUT RECOMPILING THE PROJECT?
Thank you.
You can invoke respective plugins manually:
mvn resources:resources maven-assembly-plugin:single
Though this is not the best option anyway. It's better to eliminate re-packaging all together for local deploys:
Just start the app in IDE instead of building a JAR. IDE will detect changes in the configs.
And in general allow overriding your variables with either env vars or system variables. So after reading the file also check if the values are overriden and use those.
For remote deploys we usually don't keep configuration files in JAR (you don't want to keep PRD passwords in there, right?). So deploying to remote envs should use config files from restricted sources. This means that the app needs to be able to read configs that are not inside JAR.
Here's what I added to my pom file so Maven loads the resources into the jar at the root level.
<resources>
<resource>
<directory>src/my-resources</directory>
<includes>
<include>**/*.txt</include>
<include>**/*.jpg</include>
</includes>
</resource>
</resources>
</build>
Notice how my folder, called my-resources, is not placed under main, it's placed under src. Of course, without telling Maven about your resource in the pom it won't do anything for you.
I didn't check your code, but there's how to tell Maven to pack your resources into your jar for you. And at a glance at your code, when working from inside a jar, you need to prefix your filename with a '/' because it's now using a relative path, not an absolute path.
getResourceAsStream("logging.properties") works outside a jar.
getResourceAsStream("/logging.properties") is what you need inside a jar.
I managed to solve this issue.
The other answers are correct. The real problem (I think) is with this "maven-assembly-plugin" that I was using to build.
For some reason, it does not read my properties files after build, maybe I was doing something wrong with it.
So, I changed my <build> script in my pom.xml to the following:
<build>
<finalName>myapp</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.1</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.7</version>
<configuration>
<includeEmptyDirs>true</includeEmptyDirs>
</configuration>
<executions>
<execution>
<id>copy-resources</id>
<phase>install</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target</outputDirectory>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>logs</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<excludes>
<exclude>**/*.properties</exclude>
</excludes>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>myApp.Main</mainClass>
</manifest>
<manifestEntries>
<Class-Path>.</Class-Path>
</manifestEntries>
</archive>
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/logs</include>
<include>**/*.properties</include>
<include>**/*.gif</include>
</includes>
</resource>
</resources>
</build>
In short: I replaced the "maven-assembly-plugin" with "maven-dependency-plugin" (I'm using an Oracle DB in this project), "maven-resources-plugin" (copying all of my resources to "/target" folder after build) and the "maven-jar-plugin" setting the "Class-path" property to ".".
I'm using wildfly container with arquillian for my integration tests.
In some cases, I want to use JMS and standalone-full.xml with some custom configuration is loaded at server start.
So, for my int tests, I want to load this standalone-full.xml by putting it in src/test/resources.
How can i do that ?
I can't put the following line because it's the default jboss file and not my overrided standalone-full.xml file.
<property name="serverConfig">standalone-full.xml</property>
When I specify file path (in resources), it doesn't work.
<property name="serverConfig">src/test/resources/standalone-full.xml</property>
<property name="serverConfig">/src/test/resources/standalone-full.xml</property>
<property name="serverConfig">${project.basedir}/src/test/resources/standalone-full.xml</property>
[EDIT]
When I put maven variable in surefire-plugin like this :
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<reuseForks>true</reuseForks>
<systemPropertyVariables>
<server.standalone.config.path>${project.basedir}/src/test/resources/standalone-full.xml</server.standalone.config.path>
</systemPropertyVariables>
<redirectTestOutputToFile>false</redirectTestOutputToFile>
</configuration>
</plugin>
and use it in arquillian.xml
<property name="serverConfig">${server.standalone.config.path}</property>
I have this error :
java.lang.IllegalStateException: WFLYCTL0214: Could not get main file:
D:\my_project_path\int-tests/src/test/resources/standalone-full.xml. Specified
files must be relative to the configuration dir: D:\wildfly_path\wildfly-
10.1.0.Final\standalone\configuration
We use the following configuration:
<build>
<testResources>
<testResource>
<directory>src/test/resources</directory>
</testResource>
<testResource>
<directory>src/test/resources-wildfly-embedded</directory>
</testResource>
</testResources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${version.surefire}</version>
<configuration>
<systemPropertyVariables>
<java.util.logging.manager>org.jboss.logmanager.LogManager</java.util.logging.manager>
<!-- the maven dependency plugin will have already downloaded the server on /target -->
<jboss.home>${dir.wildfly.home}</jboss.home>
<module.path>${dir.wildfly.modules}</module.path>
<!-- Do not use ./test/resources-wildfly/configuration because jboss will write to the
config directory and we don't want these files to change -->
<jboss.server.config.dir>${project.build.directory}/test-classes/configuration</jboss.server.config.dir>
<org.apache.deltaspike.ProjectStage>UnitTest</org.apache.deltaspike.ProjectStage>
<server-config>standalone.xml</server-config>
</systemPropertyVariables>
<redirectTestOutputToFile>false</redirectTestOutputToFile>
</configuration>
</plugin>
</plugins>
</build>
Within src/test/resources-wildfly-embedded/configuration we have:
application-roles.properties
application-users.properties
logging.properties
mgmt-users.properties
mgmt-groups.properties
standalone.xml
It seems you need all these files for startup to work but there is no way to put standalone.xml in a different directory than the rest of the config.
Finally, I used another method because I have multiple modules…
The build process is broken down into three parts.
wildfly container deployment
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
[...]
<artifactItem>
<groupId>org.wildfly</groupId>
</artifactItem>
[...]
<execution>
<id>stop-test-server</id>
<phase>post-integration-test</phase>
<goals>
<goal>go-offline</goal>
</goals>
</execution>
[...]
jboss_home and standalone-full.xml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<!-- Fork every test because it will launch a separate AS instance -->
<reuseForks>true</reuseForks>
<systemPropertyVariables>
<!-- the maven dependency plugin will have already downloaded the server on /target -->
<jboss.home>${project.build.directory}/${wildfly.test.embedded.folder}</jboss.home>
<server-config>standalone-full.xml</server-config>
</systemPropertyVariables>
<redirectTestOutputToFile>false</redirectTestOutputToFile>
</configuration>
</plugin>
resources copy (jms.rar and custom standalone-full.xml
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.7</version>
<executions>
<execution>
<id>copy-configuration-resource</id>
<phase>process-test-resources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/${wildfly.test.embedded.folder}/standalone/configuration</outputDirectory>
<resources>
<resource>
<directory>${project.basedir}/src/test/wildfly-resources/configuration</directory>
<includes>
<include>*.xml</include>
<include>*.properties</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
<execution>
<id>copy-deployment-resource</id>
<phase>process-test-resources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/${wildfly.test.embedded.folder}/standalone/deployments</outputDirectory>
<resources>
<resource>
<directory>${project.basedir}/src/test/wildfly-resources/deployments</directory>
<includes>
<include>wmq.jmsra.rar</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
and in arquillian.xml:
<container qualifier="arquillian-wildfly-managed" default="true" mode="suite">
<configuration>
<property name="serverConfig">standalone-full.xml</property>
</configuration>
</container>
It works fine…
So I created a windows batch file that i would like to include on my build using Maven. I placed it in the src/main/resources folder. When I do a mvn clean install, my resources end up on a folder named conf in the same directory as the jar file.
Basically I would like to have the windows batch file on the same directory as my jar, with my other resource files in the conf folder.
+ conf/
... my resource files
- myjar.jar
- myBatchFile.bat
here is a snippet of the maven plugin that does it:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>copy-resources</id>
<phase>install</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target/conf</outputDirectory>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.csv</include>
<!-- include my batch file -->
<include>**/*.bat</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
AS EXPECTED, my batch file went inside the conf folder.
Question: Is there a way to have the batch file sit beside my jar, while all other resource files inside conf?
Try to change your configuration to the following:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>copy-resources</id>
<phase>install</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target/conf</outputDirectory>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.csv</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
<execution>
<id>copy-batch</id>
<phase>install</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target</outputDirectory>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<!-- include my batch file -->
<include>**/*.bat</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
We are basically adding a further execution of the same plugin for the same phase which will take care of specific batch file and its copy while removing the batch inclusion from the previous execution.
Based on this comment I am trying to copy an empty directory from test resources folder in Maven based project to the test build output but with no luck. I have already been successfully using maven-resource-plugin for copying basic resources so I tried to add another execution part for test resources like this to my pom.xml:
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.7</version>
<executions>
<execution>
<id>copy-resource</id>
<phase>package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<includeEmptyDirs>true</includeEmptyDirs>
<outputDirectory>${project.build.outputDirectory}</outputDirectory>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
</configuration>
</execution>
<execution>
<id>copy-test-resource</id>
<phase>package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<includeEmptyDirs>true</includeEmptyDirs>
<outputDirectory>${project.build.testSourceDirectory}</outputDirectory>
<resources>
<resource>
<directory>src/test/resources</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
I also tried to define it in build section like this:
<testResources>
<testResource>
<directory>src/test/resources</directory>
</testResource>
</testResources>
but it also didn't help.
All files and non-empty directories gets correctly copied but the single empty directory doesn't.
Thanks for any help or advice.
Finally, I solved it!
The problem is that element <includeEmptyDirs> was in the wrong place in the plugin section. it should be a part of plugin configuration, NOT part of execution configuration.
Also I changed goal of copy-test-resource to testResources and outputDirectory to ${project.build.testOutputDirectory}
So the correct plugin section is following:
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.7</version>
<configuration>
<includeEmptyDirs>true</includeEmptyDirs>
</configuration>
<executions>
<execution>
<id>copy-resource</id>
<phase>package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.outputDirectory}</outputDirectory>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
</configuration>
</execution>
<execution>
<id>copy-test-resource</id>
<phase>package</phase>
<goals>
<goal>testResources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.testOutputDirectory}</outputDirectory>
<resources>
<resource>
<directory>src/test/resources</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
I have two copy tasks that I want to do with maven-resources-plugin. For example I need to copy config.yml from src/main/resources to root folder and to copy all folder contents from /src/main/resources/examples to src/examples.
root
/src
/main
/resources --> config.yml (to root)
/examples --> all folder contents (to /src/examples)
The only one solution I've found is this:
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>copy-resources</id>
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}</outputDirectory>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/config.yml</include>
</includes>
</resource>
<!-- <resource>
<directory>src/main/resources/examples</directory>
</resource> -->
</resources>
</configuration>
</execution>
</executions>
</plugin>
but I can add only one destination folder.
You can create another execution with different ID and configuration:
<execution>
<id>copy-resources-2</id>
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
...
</configuration>
</execution>