Scene.getStylesheets().add() not working inside jar file - java

As long as I run my project directly from Eclipse, I have no problem with that:
scene.getStylesheets().add(getClass().getResource("/stylesheet.css").toExternalForm());
But as soon as I run this code inside of a jar file, the resource is not found (NullPointerException).
I tried moving the css file to my src folder and then only stylesheet.cssas path instead of /stylesheet.css, but this leads to the same problem: Works fine using Eclipse, but not from the jar.
Hint: I am using Zonskis Maven JavaFX Plugin for generating the jar.

I just wasted my (your) time writing silly maven profiles.
instead of :
scene.getStylesheets().add(getClass().getResource("/stylesheet.css").toExternalForm());
simply write :
scene.getStylesheets().add("stylesheet.css");
This is how Zonski load css files.
Of course your stylesheet.css file should be in /src/main/resources, or somewhere on the CLASSPATH.

Move your file to src/main/resources and add your css file :
scene.getStylesheets().add(getClass().getClassLoader().getResource("stylesheet.css").toExternalForm());
Well, if you want to run it from the jar, then change stylesheet.css to stylesheet.bss ( binary css), package your application :
mvn clean compile jfx:build-jar
and run your jar.
java -jar app.jar
I have a ugly hack to make this a little usable (I'm using Netbeans,amazing maven integrity):
I create a project.properties file in src/main/resources directory,
file_css= ${file_css} // by default I use *.css file.
And make it filterable, in my POM file:
...
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>com.zenjava</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version> 1.5 </version>
<configuration>
....
</configuration>
</plugin>
</plugins>
</build>
...
And create two maven profiles, one for dev, and the other for production (packaging to jar):
<profiles>
<profile>
<id>production</id>
<properties>
<file_css>stylesheet.bss</file_css>
</properties>
</profile>
<profile>
<id>dev</id>
<properties>
<file_css>stylesheet.css</file_css>
</properties>
</profile>
</profiles>
So, you load your css file like this :
scene.getStylesheets().add(getClass().getClassLoader().getResource(ResourceBundle.getBundle("project").getString("file_css")).toExternalForm());
I use production profile for packaging, and dev for usual actions like compile, test, run.
Edit:
a complete example is hosted on github.

Related

How to configure Maven profiles with different resources folders?

I'm trying to configure my application to have 2 build profiles: development and production. In order to do that, I created two subdirectories under the src/main/resources folder: src/main/resources/development and src/main/resources/production. Each subdirectory has its own .properties files.
<profiles>
<profile>
<id>development</id>
<build>
<resources>
<resource>
<directory>src/main/resources/development</directory>
</resource>
</resources>
</build>
</profile>
<profile>
<id>production</id>
<build>
<resources>
<resource>
<directory>src/main/resource/production</directory>
</resource>
</resources>
</build>
</profile>
</profiles>
I build the app with the command mvn install -P ${profile_here}
Maven copies the content of the folder related to the chosen profile to the WEB-INF/classes output directory, however the development and production folders are copied as well.
WEB-INF/classes
WEB-INF/classes/development
WEB-INF/classes/production
How can I solve this problem?
Thanks in advance.
The maven-war-plugin is rather limited when it comes to resources. However, you could use the maven-resources-plugin to include/exclude resources like described here: https://maven.apache.org/plugins/maven-resources-plugin/examples/include-exclude.html

Maven dependency directory not created in target

I'm using Maven 3.3.3 to create a Spring MVC application that can be deployed to Heroku. I intend to use jetty-runner as suggested by the Heroku documentation (https://devcenter.heroku.com/articles/getting-started-with-spring-mvc-hibernate).
I can't get Maven to create the target/dependency folder in which to copy the jetty-runner.jar file. I use the commands:
mvn clean
mvn package
I get the target directory created, with all resources, web pages and source, but not the "target/dependency" folder where the jetty-runner.jar file should be copied.
My pom.xml is almost identical to the one provided by the sample application in the link above. My project and pom.xml file can be found in the following GitHub repository:
https://github.com/gfigueroa/nlp-tools-spring-heroku
Add an activation of your dependency-plugin outside the <pluginManagement> section:
...
<build>
<pluginManagement>
<plugins>
<!-- Leave as is -->
...
</plugins>
</pluginManagement>
<!-- Add the section below -->
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
</plugin>
</plugins>
</build>
...
The <pluginManagement> section is for declaring how your plugins should behave when activated. The activation itself is done outside this section.

replace classes with library classes

I have a quite large amount of java sources (an open Source Project), where i want to "patch" some class files with my own for debugging pruposes, without touching the original code.
So I made Patch-Project where i altered eg. com.package.abc.java
Now i want to tell eclipse and/or maven to build the original Project, but overwirte its own (original) com.package.abc.java from /src with the patched abc.java from my second Project, which i exported as a library.jar and added to /libs in the original Project.
The Stucture looks like this
Original:
/src
com/package/
abc.java
def.java
/libs
patch.jar
patch:
/src
com/package/
abc.java
How do i do this with eclipse and maven plugin?
Side-Node: This would be a perfect case for Dependency injection.
But to solve your problem, use the maven compiler plugin and edit the pom of the open source project:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<excludes>
<exclude>**/src/main/java/com/package/abc.java</exclude>
</excludes>
</configuration>
</plugin>
Edit: If you don't want to edit the pom of the original project (this is a bit hacky in my opinion):
Create a profile in your ~/.m2/settings.xml which contains the plugin configuration:
<profiles>
<profile>
<id>ig</id>
<plugins>
<!-- snippet from above -->
</plugins>
</profile>
</profiles>
and then build the open source project with this profile:
mvn -Pig verify

Maven Profile exclude/include directories [duplicate]

I want to use a different source directory for a specific maven profile, however, when I try to specify it in the profile definition I get this error:
Unrecognised tag: 'sourceDirectory' (position: START_TAG seen ...<build>\r\n\t\t\t\t<sourceDirectory>... )
The definition in the pom is as follows:
<profile>
<id>development</id>
<build>
<sourceDirectory>${project.build.directory}/new-src</sourceDirectory>
.
.
.
</build>
</profile>
What I am trying to do is to process the source files before its compilation if and only if this profile is active. My process will change the source files on the fly, throw the changed sources in the "new-src" directory and compile that directory as if it was the usual "src/main/java". Everything else in the lifecycle should behave normally. If this approach is flawed, could anyone point me into the right direction?
According to the documentation, you can change only few <build> parameters in the profile and <sourceDirectory> is not one of them.
I'd configure the main <build> to take sources from path defined by some property (eg. src.dir), set this property to src/main/java and override it in the custom profile:
<project>
...
<properties>
<src.dir>src/main/java</src.dir>
</properties>
<build>
<sourceDirectory>${src.dir}</sourceDirectory>
...
</build>
<profiles>
<profile>
<id>development</id>
<properties>
<src.dir>${project.build.directory}/new-src</src.dir>
</properties>
</profile>
</profiles>
</project>
See Maven model, it is not allowed to define a sourceDirectory within a profile. The only thing you can do is specify the sourceDirectory within the plugin configuration, assuming it is available.

Maven profile properties are not "overriding"

I have Maven multi-module project with such structure:
parent-pom-project
-- module1
-- module2
At the parent-pom-project I have such pom.xml
<modules>
<module>module1</module>
</modules>
...
<profiles>
<profile>
<id>local</id>
<properties>
<prop>local_prop</prop>
</properties>
</profile>
<profile>
<id>test</id>
<modules>
<module>module2</module>
</modules>
<properties>
<prop>test_prop</prop>
</properties>
</profile>
</profiles>
At all pom.xml files I have such tag:
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/test/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
At module1 and module2 in resource directory I have properties files with such text:
prop=${prop}
The problem is that after
mvn clean install
or
mvn clean install -Ptest
or even
mvn clean install -P test
I get
prop=local_prop
If I user test profile for build module2 is also builded, but properties are used from local profile.
I use Maven 3.0.3.
Anybody have any ideas?
You could try to use the mvn help:effective-pom -Ptest command to see the paramters used in your build.
See http://maven.apache.org/plugins/maven-help-plugin/plugin-info.html for more details.
Add a ${basedir} in front of your resource directories:
<directory>${basedir}/src/main/resources</directory>
This should fix your problem. My explanation would be that in a multi-module project it's not picking up the path correctly (for within the child module), if you're building from the top-level. Thus when trying to filter, it applies it to a different directory (the actual root-level aggregator), instead of the child.
I hope it helps.
I can't figure out how maven can resolve your property if you do not specify any profile. So, to see what's really there, I tried myself, following exactly the schema you described and... I did not experience the problem you have. In your case, it really behaves like if the property was defined outside the profile -as bugske suggested. What happened if you comment temporarily both profiles ?
I resolve problem uninstalling current maven plugin for eclipse and use another one.
Now I use this ones:
- Maven Integration: http://m2eclipse.sonatype.org/sites/m2e
- Maven Integration for WTP: http://m2eclipse.sonatype.org/sites/m2e-extras/
Early I was using this one http://download.eclipse.org/technology/m2e/releases/. I cannot explain such behavior but may be some configuration was changed by plugin.
Although old I had the same problem and didn't find the solution here. For me the problem was Eclipse which I use parallel to mvn on the command line. Eclipse instantly called process-resources after I did so on the command line.
Thus the solution was to select the profile in Eclipse (Project->Maven->Select Maven Profiles).

Categories