Maven build copy files form a common folder to specific folders - java

I have a folder in the file system were I have saved all the Javascripts, css and images and I am building a website with different themes. Therefore in the common folder I have all the common files that have to be copied under the specifc folders at build time.
For example I have the common folder under c:user\common with this structure
/common
--/css
----myglobal.css
--/img
----common.png
--/js
----mycommonjavascripts.js
And given a theme like this And a give theme is:
/my-theme
--/css
----mycss.css
--/img
----brand.png
----logo.jpg
--/js
----specific.js
As result I would like to have, if unzipped:
/my-theme
--/css
----myglobal.css
----mycss.css
--/img
----common.png
----brand.png
----logo.jpg
--/js
----specific.js
----mycommonjavascripts.js
Does anybody have an idea how I can achieve a result as shown above with maven?

Related

Unable to access an image while in a package [duplicate]

I have a Java Project in NetBeans 7.0.
I want to add some image to some label dynamically. The image will differ depending on the state of the program.
I put one such image, 'filling.jpg', in the 'resources' folder of my project.
I want to reach this file correctly (not by absolute or relative path, because that will cause problems when I build the jar file).
So I found this method:
ImageIcon fillingIcon = new ImageIcon(getClass().getClassLoader().getResource("filling.jpg"));
labelFontFilling.setIcon(fillingIcon);
It keeps give me java.lang.NullPointerException.
But I am sure that there is that image, because I can assign the image to the label from the NetBeans Properties menu for that label (but I don't want this, I want to add the image by Java code).
What am I doing wrong, and how can I get that image correctly?
This was a pain, using netBeans IDE 7.2.
You need to remember that Netbeans cleans up the Build folder whenever you rebuild, so
Add a resource folder to the src folder:
(project)
src
project package folder (contains .java files)
resources (whatever name you want)
images (optional subfolders)
After the clean/build this structure is propogated into the Build folder:
(project)
build
classes
project package folder (contains generated .class files)
resources (your resources)
images (your optional subfolders)
To access the resources:
dlabel = new JLabel(new ImageIcon(getClass().getClassLoader().getResource("resources/images/logo.png")));
and:
if (common.readFile(getClass().getResourceAsStream("/resources/allwise.ini"), buf).equals("OK")) {
worked for me. Note that in one case there is a leading "/" and in the other there isn't.
So the root of the path to the resources is the "classes" folder within the build folder.
Double click on the executable jar file in the dist folder. The path to the resources still works.
I have a slightly different approach that might be useful/more beneficial to some.
Under your main project folder, create a resource folder. Your folder structure should look something like this.
Project Folder
build
dist
lib
nbproject
resources
src
Go to the properties of your project. You can do this by right clicking on your project in the Projects tab window and selecting Properties in the drop down menu.
Under categories on the left side, select Sources.
In Source Package Folders on the right side, add your resource folder using the Add Folder button. Once you click OK, you should see a Resources folder under your project.
You should now be able to pull resources using this line or similar approach:
MyClass.class.getResource("/main.jpg");
If you were to create a package called Images under the resources folder, you can retrieve the resource like this:
MyClass.class.getResource("/Images/main.jpg");
Thanks, Valter Henrique, with your tip i managed to realise, that i simply entered incorrect path to this image.
In one of my tries i use
String pathToImageSortBy = "resources/testDataIcons/filling.png";
ImageIcon SortByIcon = new ImageIcon(getClass().getClassLoader().getResource(pathToImageSortBy));
But correct way was use name of my project in path to resource
String pathToImageSortBy = "nameOfProject/resources/testDataIcons/filling.png";
ImageIcon SortByIcon = new ImageIcon(getClass().getClassLoader().getResource(pathToImageSortBy));
For me it worked like I had images in icons folder under src and I wrote below code.
new ImageIcon(getClass().getResource("/icons/rsz_measurment_01.png"));

Handling resource xml files

I'm new to development and would like to clarify a couple of simple points,
When I create a new drawable xml file I place it in the "drawable-hdpi" folder and only that folder, I can access it fine like so....
android:src="#drawable/button_border"
but is that the correct correct way? or should they be in their own folder in the res folder, or just the file placed in the res folder and not in any particular sub-folder?
also if I wanted to create a arrays.xml file what folder should that be created in.
Thank you.
Your arrays should go to the values folder, so res/values/arrays.xml, as per the documentation.
As for the images, it is a good practice to provide relevant images in each drawable folder according to the screen sizes you plan to support for your app, or some of your users will end up seeing badly sized images, more details here. You can use the Android Asset Studio to generate icons, for example.
The different xml file like attr.xml, colors.xml, arrays.xml, dimens.xml, font.xml, strings.xml, styles.xml should be kept in the res/values folder.
The others custom xml file should be keep in the drawable folder.

Adding resources in IntelliJ for Maven project

I have a project structure like this:
src
|-main
|-java
|-com.abc.xyz
|-Login.java
I have to add a resource file to this and read the resource with
InputStream is = getClass().getResourceAsStream("launchers.properties");
This is giving null.
In Intellij I am not able to add a new package under src/main for resources folder so
that the project structure looks like this. How can I load the launchers.properties resource file into the project?
src
|-main
|-java
|-com.abc.xyz
|-Login.java
|-resources
|-com.abc.xyz
|-Login
|-launcher.properties
I tried the solution suggested by #maba but still not working
The launcher.properties should not be under a folder called Login. It should be placed directly in the src/main/resources/com/abc/xyz folder.
It is really as simple as I said but if the resources folder is not marked as a sources folder then this may be the problem.
This is the initial class and setup:
Now create the resources folder:
This newly created folder should be automatically marked as a sources folder and if it is blue color marked then it is. Otherwise you'll have to mark it manually:
Now you'll be able to add packages to it:
And now you can add the file to it:
And rerunning the application will not give you any null value back:
And the package view will surely show the launchers.properties file as well:
As #maba pointed out, your properties file should be in the same package as your class for your code to work.
So, you should have two files:
src/main/java/com/abc/xyz/Login.java
src/main/resources/com/abc/xyz/launcher.properties
If IntelliJ is showing the resource or not is beside the question. What you need to do is check if the results are included in your target artefact.
Do a build all in IntelliJ, open up the resulting WAR/JAR/EAR with your favorite ZIP viewer and browse into the "com/abc/xyz" folder. You should see both files there.
If they are, you are doing something wrong in your code. Check for typos, especially dots and spaces at the end or beginning (e.g. "launcher.properties[space]"), copy/paste the file name to make sure
If they are not there, your IntelliJ setup is wrong. Resources do not get included in your target build. Check online for tutorials how to do this with IntelliJ idea.
Follow these two steps
1) Create a directory
Right Click ==> New ==> Directory
2) Mark Directory as Resources Root
Right Click on the Direcory ==> Mark Directory as ==> Resources Root
No..... the structure is wrong.... you should not create the same package under resources, that is ugly and not proper: resources is for resources, and should not contain source packages.
When using ClassLoader.getResources(asStream)(path) method, the method just starts searching from the root of the classpath and the path name cannot start with / as leading characters. What you have to do, is to mark the resources as resources folder in IntelliJ. Then the files under resources will be listed under classpath and you can easily use them like you have done.
(I see in previous answers this option is not available yet in 2013, you only have mark as source folder, just like in Eclipse till now we have "add source folder", but now in 2018 it is available in Intellij: you can mark a folder as source, resources, test source, test resources, and all of them will be add to the root of classpath. )
I had the same problem and noticed that the resource file, for example: my.properties is not copied to the corresponding module folder in the target directory after build occurres. In order to solve that, I had to instruct Maven to copy the resources from the module directory to the target directory during the build process. In the .pom file I added <resource> element like that:
<project ...>
...
<build>
...
<resource>
<directory>src/main/java/com/abc/xyz</directory>
<targetPath>com/abc/xyz</targetPath>
</resource>
</build>
...
</project>
Note that the <directory> element is relative to the location of the .pom file , i.e. the root directory of the project, and the <targetPath> element indicates the package name separated by slashes.
from menu Run/edit configuration
in VM option you should add
-Dspring.config.location=path-file
I've tried it in IntelliJ, and it works!
Only solution worked for me:
File -> Project Structure -> Modules -> Dependencies Tab -> + Sign -> JARs or directories -> select resources directory -> Classes

How to group a folder structure in a common navigator view in an eclipse-rcp application?

I have a question concerning the folder structure of a common navigator view (package explorer) in an eclipse-rcp application.
My goal is to group the source folder of a project.
As you can see in the picture above the source folder of the project “Sample” is src + main + resources. I would like to group that source folder so that there is only one line displaying the text “src.main.resources”. This should not be reached by changing the folder structure in the file system.
So my question is: Is it possible to group the source folder programmatically without touching the physical folder structure?
Any help would be appreciated!
Cheers!
After doing a lot of research, I'm trying a different approach. I am using a decorator class to modify the label of the source folder.
To reach the goal of the original question, I would like to hide the first two levels of the source folder so that there is only one line displaying "src/main/resources". Sadly, IFolder.setHidden(true) does not work because it also would hide the children of the node.
Is there a possibility to hide these two folders by using a filter for example? Or do I have to modify the Tree of the Navigator?
Thank you in advance!
There are a number of resources and articles you can look into:
http://help.eclipse.org/juno/index.jsp?topic=%2Forg.eclipse.platform.doc.isv%2Fguide%2Fcnf_config.htm
http://scribbledideas.blogspot.dk/2006/05/building-common-navigator-based-viewer.html
http://scribbledideas.blogspot.dk/2006/05/building-common-navigator-based-viewer_22.html
http://scribbledideas.blogspot.dk/2006/06/building-common-navigator-based-viewer.html
http://scribbledideas.blogspot.dk/2006/06/building-common-navigator-based-viewer_18.html
http://scribbledideas.blogspot.dk/2006/06/building-common-navigator-_115067357450703178.html
With this information, you should be able to do everything with CNF :-)

How to correctly get image from 'Resources' folder in NetBeans

I have a Java Project in NetBeans 7.0.
I want to add some image to some label dynamically. The image will differ depending on the state of the program.
I put one such image, 'filling.jpg', in the 'resources' folder of my project.
I want to reach this file correctly (not by absolute or relative path, because that will cause problems when I build the jar file).
So I found this method:
ImageIcon fillingIcon = new ImageIcon(getClass().getClassLoader().getResource("filling.jpg"));
labelFontFilling.setIcon(fillingIcon);
It keeps give me java.lang.NullPointerException.
But I am sure that there is that image, because I can assign the image to the label from the NetBeans Properties menu for that label (but I don't want this, I want to add the image by Java code).
What am I doing wrong, and how can I get that image correctly?
This was a pain, using netBeans IDE 7.2.
You need to remember that Netbeans cleans up the Build folder whenever you rebuild, so
Add a resource folder to the src folder:
(project)
src
project package folder (contains .java files)
resources (whatever name you want)
images (optional subfolders)
After the clean/build this structure is propogated into the Build folder:
(project)
build
classes
project package folder (contains generated .class files)
resources (your resources)
images (your optional subfolders)
To access the resources:
dlabel = new JLabel(new ImageIcon(getClass().getClassLoader().getResource("resources/images/logo.png")));
and:
if (common.readFile(getClass().getResourceAsStream("/resources/allwise.ini"), buf).equals("OK")) {
worked for me. Note that in one case there is a leading "/" and in the other there isn't.
So the root of the path to the resources is the "classes" folder within the build folder.
Double click on the executable jar file in the dist folder. The path to the resources still works.
I have a slightly different approach that might be useful/more beneficial to some.
Under your main project folder, create a resource folder. Your folder structure should look something like this.
Project Folder
build
dist
lib
nbproject
resources
src
Go to the properties of your project. You can do this by right clicking on your project in the Projects tab window and selecting Properties in the drop down menu.
Under categories on the left side, select Sources.
In Source Package Folders on the right side, add your resource folder using the Add Folder button. Once you click OK, you should see a Resources folder under your project.
You should now be able to pull resources using this line or similar approach:
MyClass.class.getResource("/main.jpg");
If you were to create a package called Images under the resources folder, you can retrieve the resource like this:
MyClass.class.getResource("/Images/main.jpg");
Thanks, Valter Henrique, with your tip i managed to realise, that i simply entered incorrect path to this image.
In one of my tries i use
String pathToImageSortBy = "resources/testDataIcons/filling.png";
ImageIcon SortByIcon = new ImageIcon(getClass().getClassLoader().getResource(pathToImageSortBy));
But correct way was use name of my project in path to resource
String pathToImageSortBy = "nameOfProject/resources/testDataIcons/filling.png";
ImageIcon SortByIcon = new ImageIcon(getClass().getClassLoader().getResource(pathToImageSortBy));
For me it worked like I had images in icons folder under src and I wrote below code.
new ImageIcon(getClass().getResource("/icons/rsz_measurment_01.png"));

Categories

Resources