Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

jars in eclipse library and in Maven dependency

 
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a j2ee project in eclipse and I have converted it into a Maven project using Eclipse configure feature.  It creates a pom.xml and I created bunch of <dependency> that matches to most of the .jar files that were originally under eclipse "java build Path"--> library.  I have two questions:

1.  In "java build Path"--> library, I still have few customized jar files, when I right click pom.xml-->run as configuration (where I set maven clean/install to run), it compiles code well.  Now, is it true this pom.xml compile only uses maven's dependency library WITHOUT using any of the customized jar files that are under eclipse "java build Path"--> library ?

2. When I click eclipse project -> build all, it also compiles project, is it true this compile uses a combination of those customized jars under eclipse "java build Path"--> library AND maven dependency lib ?  I see both of them under eclipse "java build Path"--> library, so I assume eclipse uses both of them when compiling the project.

3. In case I do need some customized .jar file or I have some .jar that I can't find any maven repository to match it,  is it ok to leave those few jar files in eclipse "java build Path"--> library ?   But how does it work if I use pom.xml ?  Does pom.xml support such scenario ?
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've been looking into this and I've got to say, it's more complicated than it should be!

First off: Putting a jar in the Java Build Path of an Eclipse project is not going to work.  In fact I couldn't find a way to do this without using the command line.  

So if the goal is that you want a way to include an external jar file that allows you to create a project that is self-contained, I found this is the best solution.

1) In your project directory, create a folder to hold your external jars.  For instance, I created a directory "libs" in the same folder as my POM file is in.

2) Now you need to install the external jar into the local directory ("libs" in this case).  You do this with a Maven command on the command line.  First, decide on a groupId, artifactId, and version for your jar.  Then on the command line, in the same directory as the POM file is in, issue this command:

   mvn install:install-file -Dfile=<path-to-external-jar> -DgroupId=<your GroupID> -DartifactId=<your artifactId> -Dversion=<your version> -Dpackaging=jar -DgeneratePom=true -DlocalRepositoryPath=<path-to-your libs-directory>

For instance, I was using an external jar called joda-time-2.9.9.jar and I'm on Windows, so my command looked like this:

   mvn install:install-file -Dfile=C:\xxx\joda-time-2.9.9\joda-time-2.9.9.jar -DgroupId=org.joda -DartifactId=joda-time -Dversion=2.9.9 -Dpackaging=jar -DgeneratePom=true -DlocalRepositoryPath=libs

3) Now "libs" is your local repository and since it's in your project directory, it will be included in your project.  You need to tell Maven where this new repository is.  So in the POM file, add something like this:



4) Finally, you tell Maven that a dependency to your external jar exists:

 
Saloon Keeper
Posts: 27763
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In a clean Maven environment, every jar that can be used to build a Maven product should be a Maven product. Obviously, there are cases where this isn't going to be a given - for example, I've had to include IBM's DB2 driver jar as a Maven dependency in one or 2 projects. and IBM built that jar, not me, and they didn't put it in any of the global Maven registries that I know of.

So I did exactly what Knute recommended - took the DB2 driver JAR and registered it in my local Maven repository so that I could then request it as a standard Maven dependency.

One thing to note when building JEE products is that the JEE APIs are usually in 2 parts - a declaration (API) part, and an implementation part. The implementation part is normally built into your J2EE/JEE appserver, but you need to be able to reference (though not include!) the API libraries in order to do the build. For a Maven project, therefore, you'd incorporate the requisite API jars in "provided" scope - meaning that the API is going to be searched for in the repository for compilation, but not included in the WAR, since it's "provided" by the appserver.
 
Linwood Hayes
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks.  I found maven external dependency is also helpful (https://www.tutorialspoint.com/maven/maven_external_dependencies.htm).  

<dependency>
        <groupId>ldapjdk</groupId>
        <artifactId>ldapjdk</artifactId>
        <scope>system</scope>
        <version>1.0</version>
        <systemPath>${basedir}\src\lib\ldapjdk.jar</systemPath>
     </dependency>

Above is an example from that link.  it works well for those special jars.
 
Tim Holloway
Saloon Keeper
Posts: 27763
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I do not recommend using DOS-style file paths (with backslashes). Aside from the fact that backslashes are escape characters in Java, and thus perilous themselves, the backslash notation is OS-dependent and therefore breaks the idea that a Maven project should be portable. Use the forward-slash path format. It works all all OS's, including windows.

I'm also cynical about the systemPath option. Maybe it works, but my experience in the past has been that Maven didn't like such things.

Beyond that, there are a couple of reasons why this particular solution should be a last resort.

1. If you have multiple projects using this jar, having the jar as a permanent part of the project means that you have to waste disk space keeping a separate copy of the jar in each project.
2. Also, if you have multiple projects using this jar, every time that jar changes, you'll have to root through your projects and update each one manually. True, a true Maven repo would require updating the version number anyway, but it's a bit more work this way.
3. If you have multiple people working with the jar, you've got to make sure that updated versions of the jar get properly passed around and installed. In contrast, a central Maven repo allows everyone access at all times.
4. If the jar is something you're creating in-house, why is it not a Maven product as well? In which case the "mvn install" goal is all you need to push it to your local repo, and it's hardly any more work to publish it to an external repository.
 
Knute Snortum
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What I've heard is that system scope has been deprecated.
 
Tim Holloway
Saloon Keeper
Posts: 27763
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Knute Snortum wrote:What I've heard is that system scope has been deprecated.



https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#System_Dependencies

Also:

To discourage bad behavior, the Maven contributors intentionally refused to make pathname expansion work correctly in the context of the <systemPath> tag in the system scope. In other words, ${basedir}/lib/foo.jar below will not resolve:



Which reflects what I remember, and makes me further doubt that the example systemPath actually works.

The reason why system scope and filesystem paths is discouraged is because a standard Maven project should be able to be cleaned, zipped, and sent to Outermost Mongolia (or if you're Mongolian, Trenton, NJ) and the recipient should then be able to immediately reconstruct a working copy of the project using a single Maven build command (assuming you have proper access to all the dependency repositories). The recipient isn't dependent on the project creator's OS, network, filesystem organization or personal foibles - and those last two were killers in the shop I worked in pre-Maven. Code re-use was essentially impossible because people had conflcting file locations set up on their machines. And the systemPath negates that magic.
 
For my next feat, I will require a volunteer from the audience! Perhaps this tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic