create jar file with xml file inside using maven [duplicate] - java

This question already has answers here:
Include xml files in maven project
(2 answers)
Closed 4 years ago.
So I've been trying to deploy a project as a jar and it needs to read a file this way:
private static final String DEFAULT_PATH = "org/some/thing/cool/necesaryFile.xml";
public void readSomeFile() {
InputStream is = null;
try {
ClassLoader classLoader = SomeOtherClass.class.getClassLoader();
is = classLoader.getResourceAsStream(DEFAULT_PATH);
}
catch (IOException e) {
e.printStackTrace();
}
finally {
safeClose(is);
}
}
public static void safeClose(InputStream is) {
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
I've tested this inside Eclipse and it works fine! Debugging I saw the path and there was no problem at all!
When I deployed the jar with maven it showed no error, but when I try to run project A with the jar as a dependency, I get
java.lang.IllegalArgumentException: InputStream cannot be null
And checking the jar inside project A, I can't find the necesaryFile.xml that was there before deployed.
The path for the xml file was like this:
C:/coolUser/workspace/src/main/java/org/some/thing/cool/necesaryFile.xml
so it was inside cool package with some java class.
So my question is, is there a way to tell maven not to erase the file (if that's even what's happening) when deploying the jar? Moving the location of that xml file is not an option (or at least I hope not to do it)
The maven command :
mvn clean deploy -U -Dmaven.test.skip=true -DaltDeploymentRepository=coolRepo::default::${REPO_LOCATION}

Place your .xml file in src/main/resources not in src/main/java. The recommended way is to keep them in resources. If you still want to keep them along with java class, then you have to explicitly tell the plugin (which you are using in your pom to build jar) to inlude xml files.

Related

Get directory path of maven plugin in its own Mojo

I am creating a custom maven plugin. In one of its Mojos, I am reading a Xpp3Dom object from XML file using following code piece:
File pluginsFile = new File(
"absolute-path-to-file/plugins.xml");
Xpp3Dom Xpp3DomObject = new Xpp3Dom("plugins");
try {
FileReader reader = new FileReader(pluginsFile);
Xpp3DomObject = Xpp3DomBuilder.build(reader);
reader.close();
} catch (Exception e) {
// TODO throw exception
}
The XML file from which I am reading (plugins.xml) is stored in src/main/resources of the maven plugin itself. My question is, how do I point to that XML file without explicitly stating the absolute path to that file?
To be clear: I want this file to be under the directory of my maven plugin. It cannot be outside the maven plugin as it is a necessary part of the maven plugin and should not be changed by other maven projects that consume this plugin.
I have searched for a variable/method in Maven Mojo that would give me the absolute location of the maven plugin itself. If I get that, then I can just give the location as value-of-that-variable/src/main/resources/plugins.xml. But I am unable to find such variable. I have also tried for a way to pass properties from Maven plugin POM to one of its Mojos so that I can pass project.build.directory, but cannot find a way.
To be clear: I want to access a file that is under the maven plugin directory, in one of its Mojos.
Any suggestions will help.
I think the easiest form to read some of the own plugin's resources files is through the getResourceAsStream() API, because sooner or later, your plugin will be delivered as a JAR, and then the src directory will dissapear and there will remain only classpath resources:
try (InputStream input=getClass().getClassLoader().getResourceAsStream("plugins.xml")){
try(Reader reader=new InputStreamReader(input));
{
Xpp3Dom Xpp3DomObject = Xpp3DomBuilder.build(input);
} catch (Exception e) {
...
}
}
Anyway, in this way there is a risk that some other JAR of the classpath should contain a plugins.xml file by chance. To avoid (or at least reduce) this risk, you should package it:
src\
main\
resources\
foo\
bar\
MyMojo.java
plugins.xml
... and in this case, you must read it through getClass().getResourceAsInputStream().

Third party JAR's in java

I included the 3rd party jar file in the java project but still this is showing compile time error.
I included the path of the jar file in add external jar's in java.
package com.aamir;
import java.io.FileReader;
import java.io.IOException;
import au.com.bytecode.opencsv.CSVReader;//compile time error occurs here.
public class ReadCSV3 {
public static void main(String[] args)
{
CSVReader reader = null;
try
{
//Get the CSVReader instance with specifying the delimiter to be used
reader = new CSVReader(new FileReader("SampleCSVFile.csv"),',');
String [] nextLine;
//Read one line at a time
while ((nextLine = reader.readNext()) != null)
{
for(String token : nextLine)
{
//Print all tokens
System.out.println(token);
}
}
}
catch (Exception e) {
e.printStackTrace();
}
finally {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Your problem is that you are including the source only JAR file. That's the problem right there. You have the sources only (uncompiled source) JAR file and not the binary JAR (it must contain the .class files). You need opencsv-2.2.jar instead.
Here is a zip file of the binary JAR.
I included the path of the jar file in add external jar's in java.
You need to add it to the project.
You didn't mention exactly how to added the jar. Can you please post the screenshot of
Right click project --> Build Path --> Configure --> Libararis (3rd Tab ) --> Add External Jar
You should add the jar here. Please do so if not already done.
What development tools are you using? Are you compiling at the command line or in an IDE?
If at the command line, you need to include the third party JAR in the CLASSPATH - either as an environment variable or as an argument to javac.
If in an IDE such as Eclipse, you need to specify the add the JAR to the build path in the project.
Firstly, just for sanity's sake, double-check that the JAR is in the build path - that you have given it the right name and the right path - you should see it in the Referenced Libraries section of your Java project if you expand it in the Project View. Try removing it and adding it again.
Then, secondly, check that the class you are importing exists - that you have got the right package name and class name (it is case sensitive). You should be able to expand the JAR in Eclipse and see the packages and classes inside it.
Thirdly, if you are certain that everything is correct, try to use Eclipse's auto-import feature - delete your import line and press CTRL-SHIFT-O (for Organise Imports) and see if it imports the right class.
If it is not auto-importing, then it indicates that the class is not in the class path. Try steps 1 and 2 again.
Also, try cleaning Project --> Clean --> Clean all projects

Find resources in project tree

I have a plugin project and I need a method that will find and open a file in the project where the plugin will run.
I have in a variable the name of the file which should be inside the project. How can this be done ?
Thank you !
For a file resource inside a plug-in:
Bundle bundle = Activator.getDefault().getBundle("com.your.plugin.name");
if (bundle != null) {
URL fileURL;
try {
fileURL = FileLocator.toFileURL(bundle.getEntry("/path/to/your/file/from/the/plugin/root"));
String fileSystemPathToYourFile = fileURL.getPath());
} catch (IOException exception) {
// ...
}
}
Of course, you'll have to make sure the corresponding resource is properly included in your plug-in's binary build (bin.include property in build.properties file, or Binary Build section in the Build tab of the plug-in manifest editor).
If your file is a JAR, you may also have to export your plug-in as a directory (not 100% sure, may depend of what you actually do with your JAR resource - like add it to a project's build path or to a launch configuration classpath).

How to include plain files and folders into Eclipse product/plugin?

I have a plugin, which is using some plain text and binary files when running.
This is because plugin is using some third-party code, which works as in conventional application, i.e. taking data from within application directory.
When I was running plugin from within Eclipse, these data was just laying inside project directory in some folders.
To access this data I was using code like
public static final String CorePropertiesPath = "conf/core.xml";
public static URL CorePropertiesURL;
//...
Bundle bundle = Platform.getBundle(ID);
CorePropertiesURL = bundle.getEntry(CorePropertiesPath);
try {
CorePropertiesURL = FileLocator.resolve(CorePropertiesURL);
} catch (IOException e) {
e.printStackTrace();
}
I.e. to access data from file in "core/core.xml" in my project's directory, I was first converting it with getEntry() method and then with resolve() method.
This was working.
But when started to create products, I found that my files like "core/core.xml" just absent in target directory. Probably they should reside in my bundle jar, but they are not there.
How to force them to come in prescribed place?
Check build.properties file (you can edit it on the 'Build' tab of a manifest editor). Add
Eclipse-BundleShape: dir
in your MANIFEST.MF if you want to generate a directory and not a jar for your bundle.

Unable to load .so file

I have an application which uses one native library "libSample.so" which is depend upon another .so file.I am trying to load that library using following code
File File1 = new File("libSample.so");
static
{
try {
System.load(File1.getAbsolutePath());
} catch (UnsatisfiedLinkError e) {
System.out.println("Link Error");
}
}
Before loading library I have tried setting up LD_LIBRARY_PATH where the library is located using command line.
export LD_LIBRARY_PATH=/home/usb:${LD_LIBRARY_PATH}
But still the library not get load.
What should I do now?
Please help.
static {
System.loadLibrary("libSample.so");
}
I assumed that you have your jars in /libs directory and .so file in /libs/armeabi directory so the system finds them. You do not have to add .so files in your eclipse build path.

Categories

Resources