简体   繁体   中英

How can I share common components between Java web applications?

Environment

  • IDE: Netbeans 6.9
  • App Server: Glassfish 3
  • Frameworks: Spring, Hibernate, Struts 2

Problem

I have 2 web applications. I want to share resources between them both - ie authentication form jsp and other assets (js - yui,jquery/images/css). I will be adding more web applications that will also require access to these common components.

As a last resort I will create another web project with just the common assets - including them via http://common.mydomain .

How has anyone else achieved this? Can I create a web resource jar/war and include this in both applications?

I am experimenting so will update question with any progress.

Many thanks.

Netbeans has a feature to do this. Create a new project just for your shared components. List the project as a dependency of the projects that need it and make sure both projects are open in the IDE during the build process.

The IDE agnostic way(which I'm using in my own projects.)
would be to:

  1. use maven to manage your projects
  2. build your shared components into their own .jar
  3. setup a local repository server(nexus, artifactory) server to host the shared .jar
    (if you're the only dev, you might be able to get around this with mvn install )
  4. list your new jar as a dependency on the projects(wars) that need it.

Place these 'common' components in a separate module (or in several separate modules) and make a dependency to this module from other parts of your app. (I'm not sure how is it called in NetBeans, in IntelliJ IDEA it's called module, in Eclipse - it's a separate project in the same workspace).

In eclipse you must create an utility project and an ear project containing the utility project and your current web applications. You can then move common code to the utility project, and deploy the ear to glassfish.

Common resources must be located through the classpath. This is rather easy with JSF 2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM