Hi I have module inside a project which i'm trying to run from the modules directory as such:
mvn integration-test -PmyProps
in pom
<properties>
<user>admin</user>
</properties
in src/test/resources/test.properties
user=${user}
When i execute the test i get user={user} instead of user=admin
If i run it via intellij or eclipse i have no problems with it and properties get picked up?
is it because the property values are getting assigned to the target directory?
This is the class loader:
InputStream testPropertiesInput = WebDriverConfiguration.class.getClassLoader().getResourceAsStream("smoke.properties");
i've tried making it "target/classes/smoke.properties" but no luck.
First if you have unit tests which seemed to be in your case use simply:
mvn test
furthermore to get resource files beeing filtered you need to active filtering
<build>
<resources>
<resource>
<directory>...</directory>
<filtering>true</filtering>
</resource>
...
</build>
Related
I have a Java Maven web app project that I'm trying to clean up. In the <build> section of my pom.xml, I have the following:
<build>
<resources>
<resource>
<filtering>true</filtering>
<directory>src/main/resources</directory>
</resource>
</resources>
<filters>
<filter>profiles/${build.profile.id}.profile.properties</filter>
</filters>
[...other properties...]
</build>
In my project, which on my Mac is /Users/anthony/workspace/my-project/, i src/main/resources/profiles/I have local.profile.properties and qa.profile.properties.
Then, in my maven profiles in my pom, I define ${build.profile.id} as the following:
<profile>
<id>local</id>
[...stuff...]
<properties>
<build.profile.id>local</build.profile.id> <!-- or qa -->
[...stuff...]
</properties>
</properties>
When I am in my console and run $ mvn clean install -Plocal, I get the following error:
Failed to execute goal ... on project my-project: Error loading property file '/Users/anthony/workspace/my-project/profiles/local.profile.properties'.
It seems like Maven is not recognizing the resource directory for my filtering profile properties file. This only works when I explicitly put the entire path of my properties file, like so:
<filters>
<filter>src/main/resources/profiles/${build.profile.id}.profile.properties</filter>
</filters>
Is there a way for me to not have to explicitly state src/main/resources? I thought that the point of me declaring a resources directory was that I could use it, especially for declaring filtering.
The resource directory only has a meaning as "resources" for the Java artifact being built, but not for Maven itself. For Maven, the "resources" directory has a special meaning in the sense that Maven knows where to copy those files to in the resulting jar-file. But for Maven working with files or filtering files, you have to tell Maven the exact path, as Maven does not know if the filtered file is a resource, a source file, or something else. Also, you could have multiple source or resource directory defined, and Maven would not know, in which one to filter. Thus you always need to specify the full path for Maven.
So, in short:
Is there a way for me to not have to explicitly state src/main/resources?
No.
I have written this project which has a bunch of config files in config/ folder, which are necessary for running:
https://github.com/danyaljj/jwnl-prime/
The code is working fine under mvn test in my computer and CI.
But after packaging it and adding it as a maven dependency to another project, the function calls give me error
java.io.FileNotFoundException: config/file_properties.xml (No such file or directory)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at java.io.FileInputStream.<init>(FileInputStream.java:93)
at net.didion.jwnl.TestDefaults.getInputStream(TestDefaults.java:63)
at net.didion.jwnl.JWNL.initialize(JWNL.java:92)
Any idea why I am getting this weird behavior?
Update:
after extracting the jar file, it turns out that the config files are not included in the jar:
Update2: After adding the suggestion by ? the config files get included in the jar file:
But still getting the same error: config/file_properties.xml (No such file or directory)
If you have project structure other than standard format you need to update pom.xml as well. If you need to include the config for build you need to add that in build section like
Refer link enter link description here
<build>
<extensions>
<extension>
<groupId>org.apache.maven.wagon</groupId>
<artifactId>wagon-ssh</artifactId>
<version>2.4</version>
</extension>
</extensions>
<resources>
<resource>
<directory>config</directory>
<includes>
<include>**/**</include>
</includes>
</resource>
</resources>
</build>
First, verify that the configuration files are actually being packaged into the jar. I would view it in an archive viewer or unpack it and see what's in it.
Secondly, you can't directly load a file from a jar using standard file opening methods. You'll have to use something like
Class.getResourceAsStream()
See here for some more suggestions:
How do I access a config file inside the jar?
I'm trying to read a properties file using the below code on a Java Project created using Maven ArchetypeId=maven-archetype-quickstart
(properties = new Properties()).load(DbCopy.class.getClassLoader()
.getResourceAsStream("config.properties"));
And it throws exception:
Exception in thread "main" java.lang.NullPointerException
at java.util.Properties.load(Properties.java:284)
at benz.bnp.db.DbCopy.main(DbCopy.java:77)
Thanks for helping !
Have a look at the target folder, if it contains your properties file. Hopefully it will not contain. To include your property file edit your pom:
<build>
<resources>
<resource>
<directory>${project.basedir}/src/main/java</directory>
<filtering>true</filtering>
<includes>
<include>**/*.properties</include>
</includes>
</resource>
</resources>
</build>
Add it to your POM.xml and configure the same for your properties file.
Another solution:
Right Click on Project in Eclipse
Select Build Path -> Configure Build Path
In Source tab : Add **/*.properties in "Included" for "src/main/java".
The problem was: maven-archetype-quickstart only sets **/*.java in the "Included" list.
Assuming your config.properties is under src/main/resources try something like this :
Properties props = new Properties();
try(InputStream resourceStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("config.properties")) {
props.load(resourceStream);
}
I have a issue with some tests in sibling modules.
I have the following folder in module 1 in resources/: sql/statements
all the files in that folder is loaded with the following code:
URL url = this.getClass().getClassLoader().getResource("sql/statements");
assert url != null;
Collection<File> files = FileUtils.listFiles(new File(url.getPath()),
FileFilterUtils.suffixFileFilter("sql.xml"), TrueFileFilter.INSTANCE);
This works fine:
inside intellij for the sibling module
inside intellij in the module with the xml files
with maven clean install in the module with the xml files
but for some reason when I reference this class in a test in the sibling module it is unable to find the xml files when I run clean install from command line.
how can I solve this?
edit: when it fails it print the follwing url path, which seem ok (?):
url: jar:file:/home/<user>/.m2/repository/<path>/1.0.0/<jar-name>1.0.0.jar!/sql/statements
Check your POM files : you are probably only copying the resources in the build of your "sql/xml" module. Better move it up to the parent POM if you want to use it in sibling modules.
As for this maven doc, test resources should be stored in
src/test/resources
But you can set parameter testResources to the same directory as main resources
<build>
<testResources>
<testResource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</testResource>
<testResources>
<resources> <!-- this is default setting -->
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
I have a Maven project using the Swing Application Framework and would like to inject project information from the pom.xml into my application's global resources to avoid duplication.
The base application (provided via netbeans) uses Application.title, Application.version, Application.vendor, Application.description resources etc for Window titles and about box configuration but I can't find a way to set these values programatically at run time and I'm not a maven maven so don't have the skills to inject them at build time.
Anyone have any recommendations on how best to achieve the desired result?
You could try using filtered resources. If you create a property file, say src/main/resources/com/myapp/app.properties that looks like this:
version=${project.version}
name=${project.name}
id=${project.artifactId}
Them you need to enable filtering in your pom.xml:
<build>
<resources>
<resource>src/main/resources</resource>
<filtering>true</filtering>
</resources>
</build>
Now when maven builds your project, it'll expand the property file, and place it on the classpath. Then you can just call getResourceAsStream("/com/myapp/app.properties") to read it into your app.
Whist maven does automatically create a file /META-INF/maven/$groupId/$artifactId/pom.properties, this may not have all the information you need.
You can keep those in separte property file and read it from both pom.xml and your application.
Another option is to read pom.xml file from classpath (mvn will put it in META-INF folder) and parse it from there as plain xml file.
I would go with first option.
I would try using the maven-antrun-plugin. Pass the necessary maven properties to ant and create an ant build script which modifies an application properties file or the spring context configuration directly.
Another way would be to generate a separate properties file with the properties-maven-plugin and then add this properties file to the application bundle names:
For the pom.xml to write application.properties file:
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0-alpha-2</version>
<executions>
<execution>
<phase>process-resources</phase>
<goals>
<goal>write-project-properties</goal>
</goals>
</execution>
</executions>
<configuration>
<outputFile>${project.build.outputDirectory}/application.properties</outputFile>
</configuration>
</plugin>
...
</plugins>
...
</build>
For including the application.properties into your application:
public class MyApplication extends SingleFrameApplication
public MyApplication() {
super();
addGeneratedApplicationProperties();
}
private void addGeneratedApplicationProperties() {
ResourceManager resourceManager = getContext().getResourceManager();
getContext().setApplicationClass(MyApplication.class);
List<String> bundleNames = new LinkedList<String>(resourceManager.getApplicationBundleNames());
bundleNames.add(0, "application");
resourceManager.setApplicationBundleNames(bundleNames);
}
...
}
However, I find the maven-filter-solution way more elegant.