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).
Related
I am creating the client-server application in java. I have created three modules:
Client
NetworkModels - serializable models
Server
Client should ask the server to parse the xml file, xml file should be stored on the server. I have setup two build configurations as on the screen, one for the client and one for the server:
I have added file "Employees.xml" like on the next screen, I was expected that file will copied to output directory on the build
But actually when run build, the file not copied to the ouput, all I have is jar:
I want this to be able to place a xml file in the same directory as server.jar and than run the server.jar, see this code, which tries to read the file from the same location where executing jat is placed:
private File getEmployeesXmlFile() throws Exception {
var classLoader = ClassLoader.getSystemClassLoader();
var pathToExecutedJar = Main.class.getProtectionDomain().getCodeSource().getLocation().getPath();
var tempFile = new File(pathToExecutedJar);
var currentDirectory = tempFile.getParentFile().getAbsolutePath();
var pathToEmployeesXml = currentDirectory + "/employees.xml";
var employeesXmlFile = new File(pathToEmployeesXml);
if (employeesXmlFile == null) {
throw new Exception("Employees xml file not found at " + currentDirectory);
}
return employeesXmlFile;
}
The xml file is undo server module on resources/employees.xml path, I have added this wildcards to resource patterns, hoping that file will be copied to the server output, but it not helps:
What I want is to edit file in IDE and copy it to the output on build. Currently I have placed 'employees.xml' manually in the out dir.
I want this to be able to place a xml file in the same directory as server.jar
You say you want to place it near the jar but you include it into a jar artifact - so it is packed inside the jar.
For IDE to copy the resource file to output dir: the file must be placed in the resources directory. Then see the module's output directory where IDE places this file on build.
What you can do to place it near the jar is to configure an Ant task in Post-processing tab in the artifact configuration to be run after IDE builds the artifact.
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().
I have a maven project with several dependencies that are developed internally by my team. I need some information contained on the MANIFEST.mf files of those dependencies at build time of my current project.
Just to be clear. I don't want to get information from my own MANIFEST.mf file (the one from the project I am building). I want to access information contained inside the MANIFEST.mf files of the dependencies of my project at build time.
I built a maven plugin where I can check the dependency tree and retrieve some basic information about the dependencies, however I haven't found a straight forward way of getting to the MANIFEST.mf files.
Do you have any clues on how I can access them (programatically)?
Thanks!
Use the file of the artifact to create a https://docs.oracle.com/javase/8/docs/api/java/util/jar/JarFile.html , which gives you access to the Manifest
Maybe something like this would work for you
public void readManifests() throws IOException {
URLClassLoader classLoader = (URLClassLoader) ClassLoader.getSystemClassLoader();
for(URL url: classLoader.getURLs()) {
Manifest manifest = new JarFile(url.getFile()).getManifest();
}
}
I am quite new to Eclipse 4, RCP and SWT and I am stuck on this issue :
I want to access image resources from code with a relative filepath. Problem is that the default location ./ is set to my home directory /home/name/ (I'm using Ubuntu). I have found that by creating a new File and printing its CanonicalPath.
I am used to having the default location set to the project directory, such as /home/name/workspace/project/, which is, from what I've seen so far, the default behavior in Eclipse / java programs.
I would like to keep this behavior because it seems more reliable to me (after deployment for example).
Note: I have tagged e4, rcp and swt because I'm not sure which one causes the difference.
In an Eclipse plugin (including RCP code) you should use the FileLocator class to find resources within the plugin.
You open a resource as a stream:
Bundle bundle = ... plugin bundle
IPath path = new Path("path relative to plugin root");
InputStream is = FileLocator.openStream(bundle, path, true);
You can also use FileLocator.find:
URL url = FileLocator.find(bundle, path, null);
URL fileUrl = FileLocator.toFileURL(url);
Don't forget to include your resources directory in the build.properties.
If your image is not in the plugin you can't really use relative paths.
In an e4 plugin you can inject the Bundle using #OSGiBundle:
#Inject
#OSGiBundle
Bundle bundle;
(#OSGiBundle requires a dependency on the org.eclipse.e4.core.di.extensions 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.