My pom.xml has <systemPropertyVariables> and <profiles>. Right now if I want to run it as a maven build i am using -DXXX for system property variables and -PXXX for profiles in the goal.
Now I would like to run this as JUnit test in eclipse. I found that system property variables can be given in VMArguments.
Now the problem is how to access this profile. Here is how my profile looks like
<profile>
<id>devqa</id>
<build>
<resources>
<resource>
<directory>src/main/resources/devqa</directory>
</resource>
</resources>
</build>
</profile>
I have a file in the resource location and im accessing it in my test
File testfile = new File(this.getClass().getResource("/config.properties").toURI());
I tried to give this in Active Maven Profiles but it didn't work. How to make this work
Add the resource path src/main/resources/devqa to junit run configuration class path using add projects widget. It may work.
Related
TL; DR
Maven's Resources plugin doesn't seem to respect excludes elements in the resource configuration.
Setting
I have a large Java/Dart project where I need to deploy a WAR file that has both my UI and my backend in separate JARs. I want to cut down on the size of the deployed file, and I want to drop certain folders from the WAR. Based on the plugin documentation, I thought I could simply set excludes in my plugin configuration, and it won't copy over the unnecessary folders. However, it seems the Resources plugin is outright ignoring these, despite, the Maven model package including a setExcludes function.
Current Attempts
So far, I've tried two main approaches. My configuration is as follows:
<configuration>
<outputDirectory>${project.build.directory}/${project.build.finalName}</outputDirectory>
<resources>
<resource>
<directory>src/main/webapp</directory>
<excludes>
<exclude>web.xml</exclude>
<exclude>appengine-web.xml</exclude>
<exclude>**/web/_el/*</exclude>
<exclude>WEB-INF/pages/frontend/**</exclude>
<exclude>**/_el/js/frontend/**</exclude>
<exclude>**/_el/dart/app/dashboard/lib/**</exclude>
<exclude>**/_el/dart/app/dashboard/.dart_tool/**</exclude>
</excludes>
</resource>
</resources>
</configuration>
I tried to use this config inside the execution element, as well as outside from directly under the plugin element, but both times it was ignored, and everything in the webapp directory was copied over mindlessly.
On a hunch, I did try setting filtering to true, but that just ate up all the memory in my computer, and it didn't even work - what it did process was copied over.
I also tried using the Shade plugin, but gave up on that pretty quickly, as the DontIncludeResourceTransformer only permits suffix-filtering, which is not adequate for my use case.
Question
So what am I doing wrong? Based on the docs, I believe the plugin should respect my excludes list and skip the vast majority of files, but it's evidently not doing that.
You need to use apache **maven war plugin**.
The WAR Plugin is responsible for collecting all artifact dependencies, classes and resources of the web application and packaging them into a web application archive.
It is possible to include or exclude certain files from the WAR file, by using the and configuration parameters. They each take a comma-separated list of Ant file set patterns. You can use wildcards such as ** to indicate multiple directories and * to indicate an optional part of a file or directory name.
Here is an example where we exclude all JAR files from WEB-INF/lib:
<project>
...
<build>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.3.1</version>
<configuration>
<packagingExcludes>WEB-INF/lib/*.jar</packagingExcludes>
</configuration>
</plugin>
</plugins>
</build>
...
</project>
https://maven.apache.org/plugins/maven-war-plugin/examples/including-excluding-files-from-war.html
I'd like to have some tools in my maven project which I can run from inside my IDE (IntelliJ in my case) but which are not packaged with the application. Pretty much just java classes with a main method, similar to the "test" source root but not meant for testing.
Is there any good lightweight method/best practice for achieving this?
An example for such a task would be to reset some test databases, regenerate certain files based on some 3rd party systems etc, so more like scripting tasks having access to the classpath/data model of the module.
Thanks in advance!
For those internal utility programs that I don't plan to distribute, I put them under test (a folder parallel to src) with its own class hierarchy that parallels the one under src. And then in my pom.xml, I just name src as the sourceDirectory. Would this work for your case?
<build>
<sourceDirectory>src</sourceDirectory>
<resources>
<resource>
<directory>src</directory>
<includes>
<include>*.xml</include>
<include>*.properties</include>
</includes>
</resource>
</resources>
...
</build>
P.S. My artifact is a war file. And I use Eclipse not Intellj-idea but for this question I think it does not matter.
I have environment variables in my application.properties like this spring.mail.username=${username}
The ${username} is declare in eclipse environment variable. When I build maven package and install, then deploy it to tcServer. The TC Server does not know ${username}. Another word, the environment variables do not include in the war file during build.
How do I get the environment variable in eclipse to include in war file for deployment?
Using Maven filtering as described in alexbt's answer, is the right approach for including values defined elsewhere. His example touches on including an operating system environment variable. You can extend this to Maven properties also. For example,
<project ...>
<properties>
<spring.mailuser>bob#mycompany.com</spring.mailuser>
</properties>
...
<build>
...
</build>
</project>
defines a Maven properties whose value is retrieved by ${spring.mailuser} and can be used as part
of other Maven configurations or injected as content via Maven filtering. Given this, changing
applicable.properties as follows
spring.mail.username=${spring.mailuser}
will inject the value of the property at build time.
If you wish to have a build-time variable replaced, I would suggest you to use maven filtering:
Have an environment variable (not an eclipse one):
export username=user3184890
Then, in your pom.xml, activate maven filtering on resources (assuming your application.properties is in src/main/resources:
<build>
<resources>
<resource>
<directory>${project.basedir}/src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
...
Also, change your application.properties to this:
spring.mail.username=${env.username}
or
spring.mail.username=#env.username#
I have multiple projects using similar step definition across the different projects. Hence using all step definition in single project and added as dependency jar in maven.
When I run using maven command it says :
You can implement missing steps with the snippets below:
#When("^Import a canvas from \"(.*?)\" to project \"(.*?)\"$")
public void import_a_canvas_from_to_project(String arg1, String arg2) throws Throwable {
// Write code here that turns the phrase above into concrete actions
throw new PendingException();
}
but when I add package in same project it works fine. (Even in eclipse from different projects works). Is there any way to run such scenarios from maven and jenkins?
I am using eclipse IDE. maven command I used is :
mvn -DprofileTest=cucumberID clean -P cucumberID test
cucumberID is my profile name.
Following profile I added in pom.xml
<profile>
<id>cucumberID</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<version>2.11</version>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<testFailureIgnore>true</testFailureIgnore>
<includes>
<include>step_definitions/LoginTest.java</include>
</includes>
<parallel>classes</parallel>
<threadCount>3</threadCount>
<useFile>true</useFile>
</configuration>
</plugin>
</plugins>
</build>
</profile>
You haven't specified how are you running your test suite but assuming that you have #CucumberOptions somewhere, you can just point to other projects packages like this:
#CucumberOptions(. . . glue = {
"com.company.test.package1", "com.company2.test.package2", . . .})
use classpath: prefix for the package name to solve this.
for example:
#CucumberOptions(glue = { "classpath:com.example.test.steps" })
If you will use Maven's preferred way of creating packages, then "mvn package" your code, and "mvn install" this package, then you'll be able to run test from external library without changes in class annotated with #CucumberOptions.
I have added a node in my pom.xml:
<properties>
<getdownload-webapp.version>1.5</getdownload-webapp.version>
</properties>
how could I get this 1.5 value in code?
String version = System.getProperty("getdownload-webapp.version"); // output version = null
This code gave me null while running(
ps: there is no settings.xml in this project
So you have a property like this.
<properties>
<getdownload-webapp.version>1.5</getdownload-webapp.version>
</properties>
Create a file as follows in your Maven project.
src/main/resources/project.properties
Or as follows if it is for tests only.
src/test/resources/project.properties
Add this line inside the new file. Please note that you should not prefix with "properties" (e.g. don't write "properties.getdownload-webapp.version").
version=${getdownload-webapp.version}
Note that you can also add flags like this to the file.
debug=false
If not already done, you have to enable Maven filtering for your project. It is the feature that will look for placeholders inside the files of your project to be replaced by values from the pom. In order to proceed, you need add these lines inside the <build> tag of your pom.xml file. This is how to do with src/main:
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
...
And here is how to do for src/test:
<build>
<testResources>
<testResource>
<directory>src/test/resources</directory>
<filtering>true</filtering>
</testResource>
</testResources>
...
Finally, in your source code (MyClassName.java), add a block like
Properties props = new Properties();
props.load(MyClassName.class.getClassLoader().getResourceAsStream("project.properties"));
String version = props.getProperty("version");
You can add as many variables as you want to the project.properties file and load each one using this method.
The mechanism chiefly in charge to transfer Maven properties to Java application is provided by the Maven Resource Plugin
The plugin is part of the Maven Super Pom and executed during the process-resources phase of the Jar Default Lifecyle. The only thing you have to do is to active filtering.
This will replace any placeholders, e.g. ${my.property} in any of the files in src/main/resources with the corresponding property from your pom, e.g. <property><my.property>test</my.property></property>
How you make this property then available to your Java application is up to you - reading it from the classpath would work.
I assume you want to get it in the code to check something right? You can use filtering from maven that will inject the value in the source code, similar to the filtering option
http://mojo.codehaus.org/templating-maven-plugin/
String version = project.getProperties().getProperty("getdownload-webapp.version");
Where project is of type MavenProject