I need to be able to use "variables" for a maven plugin like this:
<plugin>
<groupId>com.jelastic</groupId>
<artifactId>jelastic-maven-plugin</artifactId>
<version>1.8.4</version>
<configuration>
<api_hoster>${api_hoster}</api_hoster>
<email>${email}</email>
<password>${password}</password>
<environment>${environment}</environment>
<!--<context>[specify the context if you need it]</context>-->
<!--<comment>[insert comment if you need it]</comment>-->
</configuration>
</plugin>
Already have the properties file set in the base directory and have used the plugin:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0-alpha-2</version>
<executions>
<execution>
<phase>initialize</phase>
<goals>
<goal>read-project-properties</goal>
</goals>
<configuration>
<files>
<file>${basedir}/jelastic.properties</file>
</files>
</configuration>
</execution>
</executions>
</plugin>
Still, the variables in the plugin cannot be resolved, what could be wrong here?
You must either declare those variables as <properties> in project or profile or pass them as env variables like mvn whatever -Dyourprop=value
Read about properties: https://maven.apache.org/pom.html#Properties
Read about profiles: https://maven.apache.org/guides/introduction/introduction-to-profiles.html
add those lines to your POM:
<build>
<resources>
<resource>
<includes>
<include>**/*.properties</include>
</includes>
<directory>src/main/resources</directory>
</resource>
</resources>
</build>
And this properties file:
api_hoster:${api_hoster}
email:${email}
password:${password}
environment:${environment}
and invoke any maven plugin by mentioning a profile:
mvn clean -Pname
Related
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?
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…
I am trying to build a groovy project using maven. My packaging type is war file. Maven is building the project and putting all dependent libraries in WEB-INF/lib folder but it is compiling all code into class files and putting it into WEB-INF/classes folder. Is there a way I can tell maven to build jar file for my project also and put it into WEB-INF/lib folder.
My pom.xml looks like this :
<groupId>com.myproject</groupId>
<artifactId>ExampleProject</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>My Example Project</name>
<url>http://maven.apache.org</url>
<dependencies>
...
...
...
</dependencies>
<build>
<resources>
<resource>
<directory>${basedir}/src/main/groovy</directory>
<excludes>
<exclude>**/*.groovy</exclude>
</excludes>
</resource>
<resource>
<directory>${basedir}/src/main/resources</directory>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>src/main/groovy</source>
<source>src/main/resources</source>
</sources>
</configuration>
</execution>
<execution>
<id>add-test-source</id>
<phase>generate-test-sources</phase>
<goals>
<goal>add-test-source</goal>
</goals>
<configuration>
<sources>
<source>src/test/groovy</source>
<source>src/test/resources</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
<finalName>master</finalName>
</build>
In these scenarios the usual approach is to you separate your library code in a different module that will be a dependency from your war module. For this suggestion you can see also how to generate jar and war both in project .
However if you still prefer to go with the solution you mention, you can do it with the following configuration in your pom
<configuration>
..
<attachClasses>true</attachClasses>
<archiveClasses>true</archiveClasses>
</configuration>
(see http://maven.apache.org/plugins/maven-war-plugin/war-mojo.html, and how to use class file from another war)
I have this project structure:
/src
/main
/java
/resources
/test
/java
/resources
/it
/java
/resources
test for unit tests and it for integration tests. I'm using build-helper-maven-plugin to add additional test sources/resources to the classpath for later use maven-surfire-plugin for run
unit tests and maven-failsafe-plugin for integration tests.
Plugin config as belows:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.9.1</version>
<executions>
<execution>
<id>add-integration-test-sources</id>
<phase>generate-test-sources</phase>
<goals>
<goal>add-test-source</goal>
</goals>
<configuration>
<sources>
<source>src/it/java</source>
</sources>
</configuration>
</execution>
<execution>
<id>add-integration-test-resources</id>
<phase>generate-test-resources</phase>
<goals>
<goal>add-test-resource</goal>
</goals>
<configuration>
<resources>
<directory>/src/it/resources</directory>
</resources>
</configuration>
</execution>
</executions>
</plugin>
This works fine for the test-sources (they are coppied correctly to /target/test-classes) but doesn't copy test-resources. I've tried different combinations of <configuration>: use <resource> instead <directory>, use an specific file instead a directory...but neither works.
Stacktrace with the error:
Caused by: org.apache.maven.plugin.PluginConfigurationException: Unable to parse configuration of mojo org.codehaus.mojo:build-helper-maven-plugin:1.9.1:add-test-resource for parameter directory: Cannot configure instance of org.apache.maven.model.Resource from src/it/resources
at org.apache.maven.plugin.internal.DefaultMavenPluginManager.populatePluginFields(DefaultMavenPluginManager.java:597)
at org.apache.maven.plugin.internal.DefaultMavenPluginManager.getConfiguredMojo(DefaultMavenPluginManager.java:529)
at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:92)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:209)
PROVISIONALLY, I've fixed it adding the integration tests resources to maven <build> configuration:
<build>
...
<testResources>
<testResource>
<directory>src/it/resources</directory>
</testResource>
</testResources>
</build>
But I would prefer to have centralized all classpath modifications under build-helper-maven-plugin.
Can anyone post example with a correct config?
Thanks in advance.
According to the javadoc of the maven-build-helper-plugin:add-test-resources. The resources is an array of org.apache.maven.model.Resource. Thus you must configure it this way:
<configuration>
<resources>
<resource>
<directory>/src/it/resources</directory>
</resource>
</resources>
</configuration>
Take a look at how to configure plugin parameters.