I Have Created a BndTools OSGi project that generates a .jar file.Now i want the BundleContext of that jar in my main project i.e. Dynamic Web Project (.war File).
Dynamic web Project doesn't contain bnd.bnd file.So i can't inject that jar's dependency.
Are there any ways by which i can achieve this.
This can be accomplished two ways.
Quick and dirty way
Create dynamic web project
Right click web project > Choose Properties menu
Click on Deployment Assembly
Click Add
Select Project for assembly directive type
Select the bndtools project
Repeat for 2nd project you want
Deploy this web project to server or export war and notice that the output of the
Note this jar will only create the classes and no other resources that are usually created by the bnd.bnd file (e.g. correct Manifest, other included resources)
Better and more correct way
Deploy the 2 jar files created by bndtools to some artifact repository local or public
Create a new maven project using war packaging (e.g. default web project archetype)
Specify these two bndtools project jars as dependencies
Install the m2e-wtp plugin from its updatesite for Eclipse (which imports maven war projects and automagically creates eclipse dynamic web projects with all dependency references setup)
Import the maven web project using File > Import > existing maven project
During import the m2e-wtp project configurator will be activated and the necessary settings for including your 2 bndproject jars as dependencies will be correctly setup
Related
In Eclipse, I have two projects:
archangel.core - a Maven project
ArchangelWEB - a Dynamic Web Project (built for Tomcat).
The first, archangel.core has all of the base code and uses Maven to resolve dependencies. The second is the Web addition on top of the core project. This only has code specific to presentation/view. I want to keep them separate because I may have other projects in the future that will rely on the core, and I don't want the core project to have Web Library dependencies.
Right now, in ArchangelWEB's build path, I have the archangel.core project, and I also have archangel.core in its Deployment Assembly. This allows me to refer to code from archangel.core in ArchangelWEB without any build or runtime problems.
One of archangel.core's dependencies is apache-commons-lang. Within archangel.core, I can reference classes like ExceptionUtils. However, I cannot automatically reference this jar dependency from ArchangelWEB. If I try to import class from apache-commons-lang in ArchangelWEB, it doesn't know what I am talking about.
What is the best way to import/reference dependency jars from a maven project into this other project (my Dynamic Web Project), which uses the maven project as a dependency?
Dynamic Web Projects resolves dependency only by manually placing the Jars in the WEB-INF/libs folder. There is no other way.
So you need to place the dependent jars of your Dynamic Web Project into the libs folder manually.
Don't forget to do Right-click the Jars in Lib -> Add to Build path after adding them
This is why we use maven these days instead of the old Dynamic Web Projects.
How do I add my existing GWT/GAE maven project into a new EAR project in Eclipse? So my first step is creating an EAR project with target runtime of GAE, then after that I want to add my existing GAE/GWT maven project as a default module in it. How can I do this? I only see an option to add dynamic web application module.
Thanks.
I have setup a Dynamic Web Project in Eclipse using Maven. The Web Project has a lot of dependencies to other JARs. The JARs are properly added to the WEB-INF/lib folder in Maven/Eclipse. Many of the JARs though are results of other plain Java projects. I want to have the JARs represented by their resepective Java projects in Eclipse from time to time (but not for all of them).
So how do I quickly replace a normal Maven Dependency JAR by its Eclipse project?
Currently I:
Remove the Maven Dependency (Right click on the JAR, Maven -> Exlude Maven Artifact)
Edit the Build path and add the corresponding Eclipse project
Mark the project also in the "Export and Order" section of the Build path
Edit the Deployment Assembly properties and add the Eclipse project there as well (so it goes into WEB-INF/lib at runtime).
If your web projects is a maven project (little "m" in the upper left corner) and you have installed the M2E-WTP plugin (included in the Keplar JavaEE package), this will happen automatically, i.e. when the dependent project is open in the workspace, it will be a project link, if it is closed, your web project will use the jar file as dependency.
If you are using M2Eclipse, I gess you should enable Workspace resolution.
Both projects should be maven projects.
If you're using mvn eclipse plugin, make sure, they both have a pom.xml and call :
mvn eclipse:clean eclipse:eclipse
I have several questions about creating a Java Web application with Maven and Eclipse:
How do I create a Java web project with servlets, jsp, and other classes with Maven?
It creates a simple directory structure, src->main->java. Where and how do I put the web-inf folder?
Do I need to add the jdbc-drivers manually to the folder inside the web-inf/lib, or is it ok just to point out the dependency?
Is there a way to test the servlets with junit?
Wow that's a lot of questions at once. I admit that setting up a webapp project with Maven and Eclipse can be tricky, so I'll try to answer them all.
Creating a Web application project with Maven
How do I create a java web project with servlets jsp and other classes with maven? It creates a simple directory structure, src->main->java.
When you are creating a Java web project, the final product should be a WAR or EAR file. WAR and EAR files are JAR files with a specific structure that can be deployed in an application server or servlet container.
As mentioned, the easiest way to set up a Maven project for web applications is to use archetypes:
mvn archetype:generate -DarchetypeArtifactId=maven-archetype-webapp
If we create a project with this archetype then a simple directory structure and pom.xml are generated. This project follows the standard Maven directory layout you mention, with /src/main/java/, /src/test/java, etc. Maven generates the WAR file from this structure with the war:war goal.
Note that this archetype is for a very simple (outdated) web application, but it's the best available starting point. You probably want to discard the web.xml file and create a new one that supports Servlet 3.0.
WEB-INF location
Where and how do I put the web-inf folder?
By default, Maven expects resources that should go in the root of the WAR file -- such as images, html pages and the WEB-INF directory -- to reside in /src/main/webapp/. So the WEB-INF folder should be located at /src/main/webapp/WEB-INF/. If you use the maven-archetype-webapp this directory is automatically created, along with a sample web.xml file.
Eclipse integration
You mentioned Eclipse in the question title, and it is indeed possible to develop Mavenized web applications in Eclipse with the m2eclipse plugin. Eclipse has good support for Web applications through WTP (Web Tools Platform).
Although many guides on the internet (wrongly) recommend it, you should not use the mvn eclipse:eclipse command to create the Eclipse project. This plugin can only generate WTP projects for very old Eclipse versions (WTP 2.0 is the maximum). Instead, use the m2eclipse plugin as described here.
Dependencies
Do I need to add the jdbc-drivers manually to the folder inside the web-inf/lib, or is it ok just to point out the dependency?
There is no need to do this manually, since one of the key strengths of Maven is dependency management. If you add a dependency in the pom.xml with a scope of compile or runtime, the JAR file will be automatically included in the WEB-INF/lib/ directory of the WAR file. For example to add the Postgresql JDBC driver dependency, add this to your pom.xml:
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.1-901.jdbc4</version>
</dependency>
Since the scope is unspecified Maven will assume it is in the the default scope compile. The result is that Maven will include WEB-INF/lib/postgresql-9.1-901.jdbc4.jar in the WAR file.
Testing
Is there a way to test the servlets with junit?
This question has been asked (and answered) on Stackoverflow:
Unit testing a Java servlet
Unit testing servlets
References
Hello World with JSF 2.0, Glassfish 3, Maven, SVN and Eclipse.
You should create a project based on the webapp Maven archetype, not the default one you're using.
I'm using SpringSource Tool Suite, which, for this exercise, is the same as Eclipse with m2e. Create a new Maven project and make sure you select the following archetype:
The Maven the command-line way of doing this is:
mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-webapp -DarchetypeArtifactId=maven-archetype-webapp
This archetype will put the WEB-INF folder in the correct location (under src/main/webapp).
You can find more information at http://maven.apache.org/guides/mini/guide-webapp.html
Just create a normal "Dynamic Web Project". Then right click the project name and select Configure > Convert To Maven Project. It can take up to a minute to complete the conversion, so be patient. See the following article:
http://aykutakin.wordpress.com/2013/12/04/create-eclipse-dynamic-web-project-with-maven-2/
If that doesn't work, try this:
http://crunchify.com/how-to-create-dynamic-web-project-using-maven-in-eclipse/
Step 1: create your web app folder.
Step 2: Move to that folder in command prompt.
Step 3: use following command:
mvn archetype:generate -DgroupId=com.helloworld -DartifactId=HelloWorldDemo -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false
DarchetypeArtifactId=maven-archetype-webapp will create a maven web project.
Step 4: Now on command prompt go inside the project folder and use this command:
mvn eclipse:eclipse -Dwtpversion=2.0 to make your project a dynamic web project for eclipse, now import this newly created project as an "Ëxisting project"
Use m2eclipse plugin of eclipse to enable MAVEN in eclipe.
Web.xml will be at \src\main\webapp\WEB-INF
I've few common resource files, like base JS, CSS files and HTML files and those files will be used by two maven projects/modules. I've moved these files to a common project & included that as a dependency to both projects. The common project is published as a jar files and I don't want to serve the files from jar file (using ClassLoader' stuff).
Is there a way to make the common project resource copied as it is into the actual project context folder, like:
COMMON_PROJECT/src/main/webapp/login.html to WEBAPP1/src/main/webapp
COMMON_PROJECT/src/main/webapp/login.html to WEBAPP2/src/main/webapp
Actual requirement: I don't want to publish the common files as a jar file because later those files will be moved out of war file and placed directly in Ngnix server. Can some provide the guidelines for better management in this perspective?
With Maven, you can make your common project an overlay project declared as dependency, so its resources can be shared in other projects. You will have to use the latest m2eclipse plugin with WTP integration to support overlays in Eclipse IDE.
You can provide your resources files as war project.
And then use the Warpath Plugin to make your two project import the war (and its resources).
I tried with the plugin answered by #Eugene Kuleshov, but the plugin is having dependency issues with the latest m2e version.
After some search, m2e-wtp plugin (update site: http://download.jboss.org/jbosstools/updates/m2eclipse-wtp/) is supporting the exact web-overlay feature.
This requires the latest m2e plugin (1.0 +) (update site: http://download.eclipse.org/technology/m2e/releases/)
After installing the m2e-wtp, we need to install the buildhelper lifecycle mapper from m2e catalog
Window > Preferences > Maven > Discovery > Open Catalog
install Lifecycle mappings > buildhelper
Only in the latest versions of these m2e and m2e-wtp plugins, weboverlays are supported (the old Maven builder we see in the Project properties > Builders is not capable of doing that)
However, with the normal Maven build, overlays work out of the box. By default, Maven war plugin (not eclipse plugin) will copy the resource files from dependency war (also jar) in to the context path when we Maven build the project. overlay are required if we want more control on this process, like some excluding.