Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

General questions on Maven

 
Ranch Hand
Posts: 69
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello All,

I have the following general questions on Maven:

1. What is the purpose of scope tag in the dependency section of pom.xml?
2. What is meant by classpaths with respect to maven?
3. Is a classpath in maven scopes similiar or related to the general term classpath which is the place the JDK has to look for user defined classes?
4. When exactly would I use a compile scope, which is also the default scope? Please explain with example.
 
Marshal
Posts: 5222
323
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Maven Documentation is your friend here

1. https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#dependency-scope
2. Don't understand the question but maybe to do with the directory layout https://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html
3. Don't understand the question, perhaps an example might help?
4. Same as 1 https://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html
 
Suhaas Parekh
Ranch Hand
Posts: 69
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is runtime classpath and test classpath? Is there also a compile classpath? If yes, what is it?
 
Suhaas Parekh
Ranch Hand
Posts: 69
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is meant when it is said that a dependency with certain scope will be available only during that scope? Is it not true that when a dependency is included it is needed at all times i.e compile time/run time etc?

Instead of using maven, if I manually download a dependency jar and put in the build path of a project in eclipse, with what scope is it included in the project?
 
Saloon Keeper
Posts: 24868
174
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are 3 scopes that you may commonly encounter.

compile scope puts the artefact in the compilation classpath AND includes a copy of the artefact in the target artefact.

provided scope puts the artefact in the compilation classpath, but does NOT include a copy in the target artefact. You use this for libraries that are provided from an external source. For example, the JEE API implementation JARs are part of the webapp server, so you don't want them duplicated in the WAR you build with Maven (it can cause real problems, in fact).

test scope puts the artefact in the compilation and run classpaths for when Maven runs tests, but not in the target artefact. Because test artefacts are not useful as part of a production (non-test) product.

No. Don't manually download a JAR and include it separately. At best, you'll be able to build properly only with the IDE and one of the best reasons for Maven is to be able to build when you don't have an IDE. IDEs also break more easily. If you have a JAR that's not part of the standard Maven repository and not something you build using Maven, you can catalog it into your local Maven repository and treat it just like any other Maven artefact. I often do this with JDBC driver JARs, in fact.

Maven does expect you to have a particular directory structure for your projects, and initially that's one of the reasons I hated it. Years later, I've come to appreciate the fact that someone can hand me a foreign project and I'll know exactly where to look for things.

You really shouldn't be setting a classpath for Maven builds. Maven's own classes can be found via the MVN_HOME environment variable. Everything else is stuff that Maven itself should be putting into its working classpath.
 
Sheriff
Posts: 22575
122
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Suhaas Parekh wrote:4. When exactly would I use a compile scope, which is also the default scope? Please explain with example.


The better question is, when would you not use compile scope? It's the default for a reason.

Here's my thoughts on when to use the other scopes:
ScopeWhen to use
providedLike Tim said, when the dependency is provided by the platform or server. His JEE example is one of the most common cases.
runtimeThis is the opposite of provided. Where provided dependencies can be used to compile against but are not part of your application, runtime dependencies are part of your application but you cannot use them to compile. So when do you use it? Almost never. The only example I can think of is database drivers; you use JDBC (or a layer on top of it like JPA) to compile against, not the driver itself. Note that you should only do this for self-contained applications, like Spring Boot. For JEE containers, the container should be configured with the data source, not the application itself.
testAgain, like Tim said, just for testing. The most common test dependencies are JUnit, Assert4J, Mockito, and other dependencies that just allow you to build your tests. Other test dependencies can be implementations of specifications. For instance, you have a provided dependency on the JPA specification, and a test dependency on an implementation like Hibernate.
systemWhen you have an external JAR file that cannot be downloaded as a different type of dependency. This is like provided (as in, it doesn't get automatically bundled in your application) but you have to link to the JAR file manually. You should only use this if you cannot upload the JAR file to a Maven repository like Nexus.
importWhen you have a different POM and you want to include all of its dependencies. A good example is spring-boot-dependencies. If you have a dependencyManagement block, and in it add a dependency with type import, all of the Spring Boot dependencies can be used without having to specify the versions. That's because these come from the imported POM. I would (almost) never use this scope outside of dependencyManagement, because it will really import everything. If you add spring-boot-dependencies in the regular dependencies, your application will get everything that Spring Boot offers. That's a lot, and your application will be unnecessarily large.
 
Tim Holloway
Saloon Keeper
Posts: 24868
174
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

Rob Spoor wrote:
...You should only use this if you cannot upload the JAR file to a Maven repository like Nexus.



And, as I said, it's still better if you catalog it in your own local repository. That's the first place Maven will look, followed by external (e.g., site) repositories such as Nexus, then finally in the official Maven repos.
 
Saloon Keeper
Posts: 13504
305
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Spoor wrote:This is the opposite of provided. Where provided dependencies can be used to compile against but are not part of your application, runtime dependencies are part of your application but you cannot use them to compile. So when do you use it? Almost never. The only example I can think of is database drivers; you use JDBC (or a layer on top of it like JPA) to compile against, not the driver itself.


I don't agree with this assessment. I use runtime dependencies a LOT. They're commonly used for implementations of an API.

For example, you might specify that your application is compiled against the Jakarta JAX-RS API, but you still need an implementation at runtime for application containers such as Tomcat. You can specify an implementation such as GlassFish Jersey with the runtime scope.
 
Rob Spoor
Sheriff
Posts: 22575
122
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's a scenario I hadn't thought of. I write most of my code for JBoss/WildFly, Spring Boot or Quarkus, and in all cases the implementations are already provided for me. There's no need to bundle it with the application.
 
reply
    Bookmark Topic Watch Topic
  • New Topic