Adding a Shared project to multiple projects - java

I have 3 different projects that use some global stuff
So i created a 4th project that is called "Shared Project"
Now, If i add this project to each of the other project's class path (via eclipse - > java build path - projects - > add "Shared Project"
I can debug with the shared stuff and its working well. But when i export the project into a WAR the files from the dependent project are missing
What I would like to do is - when i export a project to war it will add the shared project as a jar as well.
Is it possible?

Right-click on the spring web project, and select Properties, and select Deployment assembly. Here you can add projects . Later when you export as war, dependancy classes will be jared in to lib folder

Related

Jar missing generated pom.properties file when built in Eclipse

My project refers to the generated pom.properties file that Maven generates in:
META-INF/maven/${groupId}/${artifactId}
Which is included (in a Jar) as a dependency in another project. When I build / package the main project with Maven outside of Eclipse, the dependency Jar is built as expected (containing the pom.properties file) and all is well.
However, when I build / run the same (parent) project within Eclipse, it's not there. What am I missing?
Using Eclipse, a Jar library of the dependency is there (in the lib folder), containing everything except the Maven generated files. So in my case:
META-INF/
META-INF/persistence.xml
META-INF/MANIFEST.MF
~~~ snip ~~~
The dependency project exists in the same workspace and I have "Workspace Resolution" enabled.
Looking at the build target folder in Eclipse (\target\classes), I can see all the files that Eclipse uses for the Jar, so I can only assume that Eclipse treats it as a "regular" Java project (despite the parent project listing it as a Maven dependency in the POM) - so Maven doesn't get involved in the packaging.
How can I get Eclipse to treat the project dependency as a Maven project / so that the generated pom.properties will be included in the Jar used by the parent project?
Update
For the parent (War) project, the following structure is generated whenever I use Eclipse to build the project (using "Build Project" or "Build Automatically" not "Maven build"):
target\m2e-wtp\web-resources\META-INF\maven\${groupId}\${artifactId}\pom.properties
This is referenced in the Eclipse deployment assembly:
<wb-resource deploy-path="/" source-path="/target/m2e-wtp/web-resources"/>
So that when the War is deployed to Tomcat, the META-INF\maven... structure exists at the root of the archive.
The dependency / Jar project obviously has the same "Deployment Assembly" facility in Eclipse, so I just need to figure out what creates the m2e-wtp folder on build and apply it to that project too.
Actually, I don't think you can with the default Eclipse Jar-Export.
The best you can do if you want to do it from within Eclipse is to have a Maven Build run configuration which doesn't do much differently than using Maven outside Eclipse. But at least you have a button in Eclipse to click do generate the jar, if that's the main reason. But the normal Eclipse functionality to produce a jar (File > Export...) ignores all Maven settings.
Workspace Resolution only works for compiling and running code, but not for other Maven functionality.

External project dependency in Vaadin

I have a Vaadin project and an external Java project. I would like call code in the external project from Vaadin. I have both project imported in Eclipse and the Vaadin project references the external one in Eclipse Java Build Path. Eclipse reports no error but when I deploy the Vaadin application to Tomcat (using Project > Run As > Run on Server), I get the following error:
HTTP Status 500 - java.lang.NoClassDefFoundError: com/example/MyClass
Note that the Vaadin project is based on Ivy, not Maven.
I've tried searching around for Vaadin external project dependencies with no success. I'll be thankful for any suggestions on how to use the external project's code from Vaadin.
The solution was to add the external projects to the classpath of the Tomcat instance in Eclipse. Select
Run > Run Configurations > Apache Tomcat > (your Tomcat instance) > Classpath
Click Add Projects... and add the required project.
This will work for development. For deployment, the external project must be packaged as a library to a .jar file and copied manually (or using a build script) to WEB-INF/lib directory of the Vaadin project (see this answer on the Vaadin forum).
Another solution which will deploy your dependencies directly in the WEB-INF/lib is to :
- Add your external project to the Project / Java Build Path / Projects
- Add your external project libraries (assumed to be Maven here) to the Project / Java Build Path / Libraries
Click on Add Variable / M2_REPO and click on Extend to select the right library.
This part can be painful if you depend on many libraries.
- Add your external project and java build path entries in the Project / Deployment Assembly panel.
- Restart your Tomcat instance.
Check the content of the WEB-INF/lib directory under:
eclipse-workspace.metadata.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\your_ project\WEB-INF\lib
Take your external project and export it as a jar. Then copy your new jar and any dependencies into the WEB-INF/lib directory of your vaadin project. This will work in a deployment as well as development environment.

How to create a jar with external libraries included in Eclipse?

I am done with the project which connects to database (MySQL). Now I want to export the project as jar. But I don't know how to include its external dependencies? Is there any way of doing it in Eclipse or should I use any scripts for that?.
When you export your project as a 'Runnable jar' (Right mouse on project -> Export -> Runnable jar) you have the option to package all dependencies into the generated jar. It also has two other ways (see screenshot) to export your libraries, be aware of the licences when deciding which packaging method you will use.
The 'launch configuration' dropdown is populated with classes containing a main(String[]) method. The selected class is started when you 'run' the jar.
Exporting as a runnable jar uses the dependencies on your build path (Right mouse on project -> Build Path -> Configure Build Path...). When you export as a 'regular' (non-runnable) jar you can select any file in your project(s). If you have the libraries in your project folder you can include them but external dependencies, for example maven, cannot be included (for maven projects, search here).
You could use the Export->Java->Runnable Jar to create a jar that includes its dependencies
Alternatively, you could use the fatjar eclipse plugin as well to bundle jars together
You can right-click on the project, click on export, type 'jar', choose 'Runnable JAR File Export'. There you have the option 'Extract required libraries into generated JAR'.
Personally,
None of the answers above worked for me, I still kept getting NoClassDefFound errors (I am using Maven for dependencies). My solution was to build using "mvn clean install" and use the "[project]-jar-with-dependencies.jar" that that command creates. Similarly in Eclipse you can right click the project -> Run As -> Maven Install and it will place the jars in the target folder.
If you want to export all JAR-files of a Java web-project, open the latest generated WAR-file with a ZIP-tool (e.g. 7-Zip), navigate to the /WEB-INF/lib/ folder. Here you will find all JAR-files you need for this project (as listed in "Referenced Libraries").
While exporting your source into a jar, make sure you select runnable jar option from the options. Then select if you want to package all the dependency jars or just include them directly in the jar file. It depends on the project that you are working on.
You then run the jar directly by java -jar example.jar.
To generate jar file in eclipse right click on the project for which you want to generate, Select Export>Java>Runnable Jar File,
Its create jar which includes all the dependencies from Pom.xml, But please make sure license issue if you are using third-party dependency for your application.
If it is a standalone (Main method) java project then Not any specific path put all the jars inside the project not any specific path then right click on the project - > export - > Runnable jar --> Select the lunch configuration and Library handeling then choose the radio button option "Package required libraries into generated jar" -- > Finish.
Or
If you have a web project then put all the jars in web-inf/lib folder and do the same step.
Before exporting, click on Buildpath and configure java buildpath and add external jars inside the library. Then try to export as a runnable jar.

add other projects as library in netbeans

I have a project A (for example an Ant based Java Application project) and a second project B (a Java Class Library project or another Java Application project, both Ant build based.)
Project B has some additional Java library dependencies (.jar files in the Libraries folder).
A depends on B. A can use classes which are written by myself in B but it can not see classes in the additional libraries of B unless I add those libraries/jar files to A.
Is there a way to get NetBeans to automatically manage the library dependencies in A by including the dependencies of B in addition to B's own jar file? In eclipse when you add .jar files to project B, it is not necessary to add them to A again.
Thanks.
In your project folder you should see a folder called Dependencies, right click on that folder and choose Add Dependency.... Open the Open Projects tab; from there you can select your project from the list. Click Add.
This works for NetBeans IDE 7.2.1.
The simpler NetBeans Ant project Libraries folder is used for compile time and run time dependencies of each individual project. If your "Java Class Library" type Ant based project B has additional library dependencies, they are not automatically forwarded or updated in Project A when you include B as a library of A.
When project A is built, it's .jar file will only contain Project A code and the lib/ folder will only include .jar files from it's Libraries folder. This can cause confusion when you upgrade the dependent library versions and possibly forget to upgrade them everywhere you depend on project B.
You will need to manually manage the dependencies unless you switch to a project management and build system that handles this, like Maven.
A NetBeans Maven project will have a Dependencies folder instead of a Libraries folder and if project B is properly setup and registered as a Maven project and project A is also created as a Maven project, you could Add Dependency on B to A and B would forward it's dependency information to Maven project A.
If you want to add another project as a library in netbeans right click on the libraries directory and select "Add Project", select the project you want to add. When you do this the jar file(in the dist dir) that has been built for the project you are adding will be selected and then click the "Add Project jar files" and that should add the jar file to your project. Alternatively you could select "Add Jar" instead and add the jar file yourself the way that Ravi described.
In your project folder you see a folder called Libraries , right click on that folder from there you can select your .jar file. You need not to add .jar files repeatedly after adding once to your project. You just need to Clean and Build your project whenever new files are added to .jar file.

creating a jar for a project in Eclipse

I am using Eclipse and I have just started working on a project that needs another project (say pjkt) to function. The pjkt project files are stored in a specific folder. I would like to create a jar from pjkt and then add this jar to my project buildpath. How do I do this? Do I need create first in Eclipse a new project from the pjkt code, build it and then select the export command?
Yes create a new project and export as .jar.
To answer the question about the build path:
Create a project directory (in the using project) for library files - it's common to create it in parallel to /src and /bin and call it /lib;
Once you've created (by exporting) your jar from the used project, copy it into the /lib directory;
If you did any of this creating and copying outside of eclipse, refresh your project so everything will show up in the Project Explorer;
In the Project Explorer, right-click on the .jar in the /lib folder and do "Build Path | Add to Build Path".
Done!
If you need to work on both projects, and have them open in Eclipse at the same time, another approach is to open Properties -> Build Path for the new project, and add pjkt on the Project Dependencies tab. This will provide all the exported resources from pjkt to the master project.

Categories

Resources