Eclipse: auto execute and generate code on save - java

I'm using Eclipse and I wanted to create a system that automatically generates java source code every time I save the project. More precisely I want to search for some files in a directory, generate static attributes for each of them and generate some methods every time the project is updated/saved. I thought that a piece of code that could be auto-executed by Eclipse would be fine, but I don't know if it's even possible. How can I achieve this behaviour?

You can specify a program or Ant script to be run when a project needs building in the 'Builders' section of the Properties for a Project.
You can also write an Eclipse plugin 'Incremental Builder' using the 'org.eclipse.core.resources.builders' extension point. More information about this in the Eclipse help.

Eclipse has an extension point for cleanup and save actions that will be executed when you a save a file.
This requires implementing and installing a custom Eclipse plugin, so it's more intrusive than using a builder to run a script, but also more flexible, since you can use the JDT API to inspect the Java model of the given file.

I personally use :https://github.com/mystilleef/eclipse4-smartsave
Just from marketplace eclipse4-smartsave or as on page other solutions.
Plugin sometimes hangs, still it is great.

Related

Java: Proper organization of external libraries?

So I took a Coursera course that had me work with external libraries. Two .jar files which most of the weekly projects depended upon.
I'm not entirely sure how eclipse compiles and runs the files, and how it links to these external libraries -- what is the proper way of organizing this? Do I put a copy of each .jar file in each project directory?
Is there another, cleaner way that I should be organizing this?
Thanks --
As a beginner programmer it is OK to put it in each project. But consider that this is ongoing work and at some time in future you want to upgrade to a new version of these external libraries. Then you would have to copy it everywhere.
Instead another option is to store them in one place and add it in classpath in each project. Now you have only one copy of it, which is always better.
Now, if and when you do get a new version then the file names might change, so you will still have to change the classpath of each project.
But I advise you to worry about these and other such problems later. For now, focus on programming related problems rather than configuration.
If you want to maintain your libraries professionally in a formal manor then you're better of using a build tool like Gradle of Maven.
I'd suggest you to use Gradle to maintain the project since it has a whole lot of useful build tools available to you to use. Eclipse has a Gradle plugin available which allows you to use Gradle projects with it. See link below.
To give you a idea of how Gradle is used professionally. Android uses it by default to maintain their projects now. So Android java projects uses the Gradle build tool to maintain its library sources, compilation processes and such.
The difference between a Gradle project and a normal java project is that a Gradle project has a list of pre-defined scripts available to you which fetches the libraries, compiles them and prepares them before exporting the final bundle (jar). So really all Gradle does in before hand is fetch the libraries and prepares the specified tools before compilation so you won't need to mess with them your self. It prepares your project directory and remotely maintains your libraries so if they're available from a repository then it'll make sure to prepare them appropriately in before hand and setup your projects directories.
So really the difference you'd physically notice is that instead of using the default Eclipse export button to create your bundle (jar) you'd instead use a button from the side menu which the Gradle plugin adds and also you'd cleanly list the libraries in a structured order in a file that gets added to your project root.
If you want to get a basic understanding of how it works and really want to start to proffesionally or formally structure your project then try to create a very basic android app in Android Studio. see link below
If this isn't what you want at all and don't want to take it to this advanced level yet then adding the library bundles into some kind of lib folder that's located in your project root is properly best practice.
If you wonder why? Well basically different projects might use different versions of the library which may add or remove support to them. So to keep the versions consistent and make sure to have the right version available to you, you have the direct source near the project it self.
Here's some useful link:
http://www.vogella.com/tutorials/EclipseGradle/article.html
http://developer.android.com/sdk/index.html

Auto-Import Call in Eclipse Plugin

I'm currently writing a Eclipse plugin for model driven code generation.
This Plugin features a Wizard for converting UML to Java Code and deploying (in this case copying) the Code Base to different Projects.
One Project now has the necessity for one auto-import cleanup (the pipeline messes around with the imports). I usually do this by selecting the source folder and then pressing ctrl+shift+o (organize imports).
Is there the possibility to start this operation out of my Plugin?
in short: How to start the "organize-import" function from my plugin?
The command id for organize imports is org.eclipse.jdt.ui.edit.text.java.organize.imports so you can execute it with:
IHandlerService handlerService = PlatformUI.getWorkbench().getService(IHandlerService.class);
handlerService.executeCommand("org.eclipse.jdt.ui.edit.text.java.organize.imports", null);
The command probably expects the current selection to be a Java file.

Java. IntelliJ IDEA hotswap

I have a web project. I build it using maven(complete all stages in every module, then archiving each module to *.jar and then making war-file).
If I change one line of code in one class I need to run maven build script and it takes ~5 minutes.
How can I see my changes without building the whole application if I changes are within one class and one method?
You should use IntelliJ IDEA for building your project and its Artifacts in the exploded form, so that the classes can be reloaded with hotswap.
With such configuration you can update your application much faster. Also check the tutorials.
For even faster updates consider using JRebel.
You could consider using JRebel. You will need to have a valid license.
You can also can try open source tools just take a look this hotswaping tutorial:
http://www.asjava.com/core-java/how-to-hotswap-java-code-into-jvm-redefinition-example/

Hook into the Eclipse build process?

I'd like to be able to run a simple Java program when the build button is pressed in Eclipse. Currently when I click build, it runs some JRebel logging code. I have a program that parses the JRebel log file and stores statistics in a DB.
Is it possible to write a plugin or somehow hook into the build process of Eclipse, so that after the JRebel logging is done, it will run my program to parse the file automatically?
Any help would be appreciated, even if you could just point me in the right direction.
Thanks in advance.
You can add a custom Builder to your java project.
Right-click on your project. Select Properties.
In the tree on the left side choose 'Builders'.
You can either add an ant script or specific program. You also have ability to choose when this tool will be running ( before or after Java Builder ).
You can write an incremental project builder and register it via extension point: http://www.eclipse.org/articles/Article-Builders/builders.html

first servlet/jsp application, just need some clarifications

I first tried the spring mvc tutorial using eclipse, but got a bit frustration and just did the first few steps using netbeans.
Netbeans, using a java web template, creates folders like:
/webpages
/webpages/meta-inf/
/webpages/web-inf/
/webpages/*.jsp
/source packages
/source packages/xxxx/xxx.java
/test packages/
/libraries
/configuration files
/configuration files/manifest.mf, context.xml, web.xml
The spring tutorial suggests to create:
/appname/
/appname/src
/appname/war (jsp's go here)
/appname/war/web.xml
/appname/build.xml
/appname/build.properties
/appname/war/WEB-INF/appname-servlet.xml
/appname/src/appname/web/HelloController.java
Now my question is, if I modified my netbeans project to mirror this structure, will it break the automatic build that netbeans gives me?
I downloaded the full version of netbeans, so I'm not sure if Ant comes with it or not?
When using the IDE's build/run, I guess I am using the IDE's build engine, can I force it to use Ant somehow or its better just to use Ant at the command line?
Here's the Spring tutorial reworked for NetBeans.
One option, becaue you have Netbeans, is to do this using Maven. Maven is an external build system (and much more) that netbeans can use. Create a new project and select a Maven project. Then create a simple Java web application (or a spring application if you want to dive right in). The directory structure that is set up will be correct.
As an Eclipse user, I'm not really good with NetBeans. But I do know that the "internal build system" is based on Ant and a pretty elaborate project framework Ant file.
You could dig your way through the maze of files and targets and fix the problem manually, but my suggestion would be to adapt the Spring tutorial's names to the directory structure NetBeans gave you.

Categories