Using image resources in a jar using maven - maven

I have a java/maven project that uses tango icons, so I'm using the following dependency:
<dependency>
<groupId>org.freedesktop.tango</groupId>
<artifactId>tango-icon-theme</artifactId>
<version>0.8.90</version>
</dependency>
And the following code, which would work ok before I moved the proyect to maven:
imageIcon = new ImageIcon(this.getClass().getResource(org/freedesktop/tango/22x22/actions/address-book-new.png));
However, it now results in NullPointerException for some reason.
The generated jar does not contain the images, nor reference them in any way, but classes in other jars are.
I've investigated a lot, but I've found most people bundle their images with their source. This isn't my case.
What do I need to do for maven to reference and/or package these images propertly?
How can I get maven to build a standalone runnable jar (this is a swing application).

If you add a slash before the path it works:
ImageIcon imageIcon = new ImageIcon(this.getClass().getResource("/org/freedesktop/tango/22x22/actions/address-book-new.png"));
From the API:
Before delegation, an absolute resource name is constructed from the
given resource name using this algorithm:
If the name begins with a '/' ('\u002f'), then the absolute name of
the resource is the portion of the name following the '/'. Otherwise,
the absolute name is of the following form:
modified_package_name/name Where the modified_package_name is the
package name of this object with '/' substituted for '.' ('\u002e').

Related

Grails 3 (Gradle) dependency without parent directory

Can I not use the following Gradle approach to dependencies in Grails? I do not have nor want a parent directory;
https://stackoverflow.com/a/19303545/2288004
When I try it, I get the the following error;
Caused by: java.lang.IllegalStateException: Expected method not found:
java.lang.NoSuchMethodException:
org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedContext.addApplicationListener(org.apache.catalina.deploy.ApplicationListener)
It works when I use a parent directory for the settings.gradle, but unfortunately it’s not how I want to structure the project.
The following was indeed the solution I was looking for,
include ":myplugin"
project(':myplugin').projectDir = new File(settingsDir, '../myplugin')
The error was down to how I was managing my tomcat dependencies between the two projects.
Tomcat was already being pulled in via the plugin but while I still needed to reference tomcat at compile time in the application, I also needed to make sure it was the same version, and so added the following just above "dependencies" to target the version I required;
ext['tomcat.version'] = '7.0.70'

How to run external ruta scripts from a maven project without placing the script or its typesystem in the classpath?

Till now, I had been running ruta scripts from a maven project by creating AnalysisEngine and CAS, and processing the engine. To do this, I had placed all the scripts and descriptor files (Engine & TypeSystem) into scr/main/resources folder of the maven project.
Now I want to place the scripts and TypeSystem files in an external path and pass the path dynamically to my java code that runs the scripts. Is it possible to do it ? If so, how ?
I simply placed the files(script & descriptor) in an external path and passed the new path to instantiate the AnalysisEngine as below;
final AnalysisEngine engine = AnalysisEngineFactory.createEngine("home/admin/Desktop/TEST_ScriptFolder/com/textjuicer/ruta/date/Dazzle_ChapRef_UpdatedEngine");
Error
org.apache.uima.util.InvalidXMLException: An import could not be resolved. No file with name "home/admin/Desktop/TEST_ScriptFolder/com/textjuicer/ruta/date/Dazzle_ChapRef_UpdatedEngine.xml" was found in the class path or data path. (Descriptor: )
at org.apache.uima.resource.metadata.impl.Import_impl.findAbsoluteUrl(Import_impl.java:117)
at org.apache.uima.fit.factory.AnalysisEngineFactory.createEngineDescription(AnalysisEngineFactory.java:869)
at org.apache.uima.fit.factory.AnalysisEngineFactory.createEngine(AnalysisEngineFactory.java:107)
at com.textjuicer.ruta.date.ArtifactAnnotator.getAllAnnotations(ArtifactAnnotator.java:93)
at ApplyingStyle.XmiTransformer.parseXMI(XmiTransformer.java:33)
at ApplyingStyle.ApplyStyle.applyStyleOnDocx(ApplyStyle.java:76)
There are two layers:
The RutaEngine needs to find the scripts/resources/descriptors
UIMA needs to be able to resolve imports of descriptors
The resource lookup in Ruta has two stages, it searches for them in the absolute paths specified in the configuration parameters. If the resource is not found it searches for it in the classpath. So you need to set the configuration parameters: scripts are located in scriptPaths, descriptors are located in descriptorPaths and wordlists are located in resourcePaths. See the documentation for further information.
The problems with the imports in descriptors can be solved by either setting the datapath in the UIMA ResourceManager or by changing the import to "location" instead of "name". The datapath can be used as a replacement for the classpath. The Ruta descriptos use import by location if it specified int he ruta-maven-plugin.
DISCLAIMER: I am a developer of UIMA Ruta

Replacing classes/resources with an OSGi fragment - possible without including a jar in the fragment?

We want to replace certain resources in a host OSGi bundle by adding an OSGi fragment.
As I understand it, the resources included in an OSGi fragment are merely added to the classpath of the host bundle.
There is no guarantee that if the fragment is adding a resource that already exists in the host bundle,
the resource from the fragment will be loaded: it could also still be the host version.
In order to make sure the fragment version of the resource is loaded instead of the host version,
http://wiki.osgi.org/wiki/Fragment mentions it is possible to use the Bundle-ClassPath header to specify resources as "first"(preferred).
It gives patch.jar as an example:
Bundle-ClassPath: patch.jar,.
As mentioned there: "Since patch.jar is ahead of '.' it will allow classes to be preferentially loaded from the fragment instead of the host."
I could use this technique, but this means I first have to bundle my fragment resources in a separate jar and then include this jar in the fragment bundle.
Is there a way to preferentially load classes/resources from the fragment instead of the host without having to include a jar in the fragment?
You don't have to include a jar. You can instead use a "patch" directory and then put your classes in a patch directory in the fragment.
Ok, there are a couple of ways to accomplish what you want. As I understand it, after everything is done, you want to export packages from a library bundle except for the packages located in your patch bundle.
To accomplish this, in the Manifest.MF of your library bundle, specify the explicit packages you want to export instead of '.'. You can do this with a fragment so that you won't have to modify the original bundle. Then, do the same with your patch bundle.
Another option is to use the maven-bundle-plugin to "shade" (merge) the patch bundle and the library bundle together into a new bundle. As long as the patch bundle and he library bundle have different version numbers, this will also work. Many blogs will instruct you to use the maven-shade-plugin along with the maven-bundle-plugin for this option, but they are incorrect. You can definately perform shading with the maven-bundle-plugin.
For those that are still struggling, these are the exact steps that worked for me:
Create a fragment project with the resource/class you want to replace
In the fragment's build.properties, change source.. = src/ and output.. = bin/ to source.patch/ = src/ and output.patch/ = bin/
In the fragment's manifest, add patch/ to the bundle class-path
Example:
Let's say you have com.example.ui plug-in which has a com.example.ui.MessageDialog class that you want to replace.
Create a fragment project com.example.ui.fragment
Create the MessageDialog class in the com.example.ui package (not com.example.ui.fragment);
Edit the fragment's build.properties file like this:
source.patch/ = src/
output.patch/ = bin/
In the fragment's manifest add this:
Bundle-ClassPath: patch/
In the com.example.ui manifest change the bundle class-path:
Bundle-ClassPath: patch/,.

Path for images folder in GWT project

I have the following line of code to set the URL of an image I want to display
img.setUrl("images/img1.jpg");
Where should I place my images folder in order for it to get picked up correctly. Currently I have it in my WEB-INF folder but this does not appear to work. I get the error on my console "WARNING: No file found for: /images/img1.jpg"
I created my project using Eclipse and I have not changed any folder structure.
You should make a folder named images under your war/project_folder and place the images there.
And while setting the Url you shoud do
image.setUrl(GWT.getModuleBaseURL()+"images/im1.jpg");
You can use either GWT.getModuleBaseURL() or GWT.getHostPageBaseURL().
Create a public folder (named "public") alongside with folders "client" and "server". The content of the public folder will be copied into the war.
>src
>com
>test
>client
--->public
>images
>server
>shared
Voilà!
It is always better to use absolut URL rather than relative URL by using GWT.getModuleBaseURL(). If you want to use relative path, then your image should be in the war as described by your relative path.
In your case it is
war->images->img1.jpg
You need to put the images in the web app directory.
You can check your web app directory here
Proeect --> Properties --> Google --> web Application
Here you will find the WAR directory
So in this directory , you need to put your images.
You can put the image directly or else you can make folders as you want
sample -
image path --> WebContent/img/pics/recent/data/1/1.jpg
String imagePath = "/img/pics/recent/data/1/1.jpg" ;
Image img = new Image(imagePath);
NOTE :- How ever GWT Image does not understand absolute file path , you need to use relative file path

Dynamic file name in Maven archetype

I've problem with Maven. I tried to create archetype, but I don't now how to put source file to directory ${groupId}/${artifactId}. If I try to create project from this archetype, file is stored implicitly in ${groupId} directory. It seems Maven supposes, that group ID is a package name. But I'm using ${groupId}/${artifactId} as package name so I need have the source file stored in ${groupId}/${artifactId} directory. I have seen solution: to put the file into __artifactId__ directory. Double underscore should determine a variable. However it doesn't take any effect. Variable seems to be empty, but in package name inside source file is used succesfully.
Have enybody idea, which trick shall have been done when using double underscore in file name?
When you create a archetype of your project like so:
mvn archetype:create-from-project -Dinteractive
Set the package - in an explicit form to the root of the project.
Then you create a project of this archetype enter your root package ${groupId}.$ {artifactId} in explicit form. example:
mvn archetype: generate
-DarchetypeGroupId = com.test
-DarchetypeArtifactId = my_archetype
-DgroupId = com.test
-DartifactId = app
-Dpackage = com.test.app
-Dversion = 1.0-SNAPHOT
In this case, $ {groupId} = com.test, $ {artifactId} = app and target package = com.test.app

Resources