I am running a java/spark project from IntelliJ idea (community 2019.2) on macbook pro.
I use maven.
The scope of dependencies of modules have four types:
compile
test
build
provided
When I build the modules, I always got the error:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/spark/api/java/JavaRDDLike
Or, other classes.
I have to change the scope of the dependency of the class from
provided
to
compile
But, there are so many dependencies, it is really boring to change them one by one.
Also, whenever I use
invalidate cache and restart
All setting for the scope will be reset, I have to repeat the scope setting.
How can I only need to change scopes for all dependencies only one time and make IntelliJ remember what I have changed for scopes?
Thanks
If you are using Maven you must not change the configuration of the dependencies and other project structure settings (like directories, compiler etc) in the IDE UI. You change it in Maven pom.xml file. So to set the dependency scope in Maven dependency configuration using <scope> element e.g.
<dependencies>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
<scope>compile</scope>
</dependency>
</dependencies>
Related
in java 8 projects you simply add the following dependencys in maven
<dependency>
<groupId>javax.json</groupId>
<artifactId>javax.json-api</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.json</artifactId>
<version>1.1</version>
</dependency>
Using OpenJDK, Eclipse 2018-12
and maven module, it results in getting a error in the module-info.java:
The package javax.json.stream is accessible from more than one module:
java.json, org.glassfish.java.json
So in both dependency projects there is a package called javax.json.stream and due to jigsaw module system this is not allowed anymore?
How to fix this?
EDIT:
I updated the maven dependency to 1.1.4 and put them on the classpath.
The javax.json-api has a module-info.java file and is working fine, eclipse shows no more errors.
But now the packages of the implementation javax.json (org.glassfish) are not found, resulting in a ClassNotFoundException: org.glassfish.json.JsonProviderImpl
What more can I do?
EDIT:
Its working now, i forgot to generate a module-info.java in this project.
So in both dependency projects there is a package called
javax.json.stream and due to jigsaw module system this is not allowed
anymore?
This is still allowed, but with both those dependencies getting resolved on the classpath instead of modulepath i.e. in the unnamed module.
Another alternative to fix this while you create your library as modular is to make sure to fix the downstream libraries exporting the same package which would require a bottom-up migration and you might have to wait for them to fix it in their latest update(or check if one is already out) and then to rely on them.
I have two Maven projects, A and B, where A depends on B at compile time, but at runtime B needs some classes of A.
What I did is:
A's pom.xml
<dependency>
<groupId>B</groupId>
<artifactId>B</artifactId>
<version>${B.version}</version>
</dependency>
B's pom.xml
<dependency>
<groupId>A</groupId>
<artifactId>A</artifactId>
<version>${A.version}</version>
<scope>runtime</scope>
</dependency>
When letting Jenkins compile the projects it fails to compile each other as a downstream project because it finds the circular dependency and avoid the infinite build loop.
So, what I thought is a way to add the A's runtime dependency only when packaging B (when Jenkins executes mvn package) so that Jenkins does not find the circular dependencies in the pom.xml files and configures the downstream compilation.
Is there any way to accomplish this with an existing Maven plugin or other way?
Thank you
I wrote this Maven plugin for the same reason.
It adds any listed JAR artifact to the WAR file where this plugin is used. JAR dependencies are resolved and added to the WAR file if not artifact with the same version is found.
The important thing is to define dependencies only inside the <dependency> POM section because that's the only configuration used by the Maven reactor.
If you fiddle with custom plugins to introduce your own dependency management ideas you will most likely break the reactor. Even if your custom approach works with regular mvn clean install it will usually explode when -T4 or similar option is used to enable multi threaded builds. There is simply no way to explicitly define the module build order in POM as it's governed by the reactor.
The usual way of sharing code between modules is to create a new module C which is depended on by both A and B.
In NetBeans 8, in a Maven-based project, how does one use a jar while programming but omit from build?
I need to access some specific classes in a specific JDBC driver in my Vaadin web app. But in web apps, we normally do not bundle JDBC drivers within our build (the .war file). Instead, the JDBC drivers belong in a folder controlled by the Servlet container (the runtime environment).
So, I need the JDBC driver (a jar file) to be on the classpath while I am editing my code and compiling. But that jar file must be omitted from the build.
exclusions Tag
I tried adding the exclusions and exclusion tags to my dependency element. But this did not work – The postgresql-9.4-1201.jdbc41.jar appeared in WEB-INF/lib folder.
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.4-1201-jdbc41</version>
<exclusions>
<exclusion>
<groupId>org.postgresql</groupId> Exclude from build
<artifactId>postgresql</artifactId>
</exclusion>
</exclusions>
</dependency>
New Profile?
This Answer by ZNK - M on the Question, Setting custom runtime classpath for a maven project in netbeans, may be what I need.
But creating a new project profile seems like overkill what seems like small little task to me. And, I always want to exclude this jar from my build output, not just when testing or in other limited scenarios.
You should add a new profile run-with-netbeans in your pom that declares the additional dependencies (use the provided scope to not include them in the release).
Then you'll have to add the new profile to your IDE to run the pom with the -P run-with-netbeans option in the command line.
But I am familiar only with the basics of editing a POM file. If that approach is the way to go, it would be helpful if someone could expand on the details and steps needed.
<scope>provided</scope>
Use <scope> tag in POM file, with a value of provided.
Excerpt from the Dependency Scope section of the page, Introduction to the Dependency Mechanism :
compileThis is the default scope, used if none is specified. Compile dependencies are available in all classpaths of a project. Furthermore, those dependencies are propagated to dependent projects.
providedThis is much like compile, but indicates you expect the JDK or a container to provide the dependency at runtime. For example, when building a web application for the Java Enterprise Edition, you would set the dependency on the Servlet API and related Java EE APIs to scope provided because the web container provides those classes. This scope is only available on the compilation and test classpath, and is not transitive.
runtime[…]
test[…]
system[…]
import[…]
Like this:
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.4-1201-jdbc41</version>
<scope>provided</scope>
</dependency>
Use the provided scope instead of the default compile scope for this dependency. That's exactly what it's for.
<dependency>
<scope>provided</scope>
<groupId>...</groupId>
<artifactId>...</artifactId>
<version>...</version>
</dependency>
I'm currently learning java and want to create a project, using maven, hibernate and MySQL. I know that in order to use any of the artifacts with maven, I should find it on mvnrepository and add it to pom.xml. The question is where can I get the list of mandatory dependencies for each artifact I use, f.ex if I need hibernate, I found hibernate-core 4.3.8.Final, proceed to this link and can see it's dependencies in section "depends on". Should I add all of them into pom.xml also?
Well, I think you know about maven.
And yes, You should include all the dependencies with version on your pom.xml files (Which is the main file for all your dependencies ).
First, you need to identify all required dependencies and add on pom file.
While executing code, It primarily tries to get that dependency from local repository (.m2) And if it doesn't exists then it downloads from it's web repository.
Link: maven setup
How it works??
Suppose, You are using log4j for loggin.
You need to know the log4j Maven coordinates,
for example
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
It will download the log4j version 1.2.14 library automatically. If the “version” tag is ignored, it will upgrade the library automatically when there is a newer version.
Declares Maven coordinates into pom.xml file.
<dependencies>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
</dependency>
</dependencies>
When Maven is compiling or building, the log4j jar will be downloaded automatically and put it into your Maven local repository.
All manages by Maven.
How to find the Maven coordinates?
Visit this Maven center repository, search the jar you want to download.
Hope, It will help.
Thanks.
Say I have two Maven dependencies defined in a project like below.
<dependency>
<groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId>
<version>1.3.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mycompany.library</groupId>
<artifactId>mylibrary</artifactId>
<version>1.0.1</version>
<scope>compile</scope>
</dependency>
Then, within mylibrary, I also have a dependency defined as below.
<dependency>
<groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId>
<version>1.3.1</version>
<scope>compile</scope>
</dependency>
When I package my project, I don't see xstream packaged within it. I think the project's xstream dependency scope, 'test' is overriding the mylibrary's xstream dependency scope, 'compile'.
In this kind of situation, what's the best way to include the xstream for the whole project so the submodule can have access to it when packaged within the project?
I've read Apache Maven website's explanation on Transitive dependencies, but I'm struggling to understand what it means, and also to find out the best practice in this situation.
This feels really odd to me, and if it's "feature", I think it is a really dangerous one.
Anyway, it's not a Maven bug and it's in the maven documentation here.
Regarding best practices on this issue, I haven't heard of any, but the safest way to proceed ought to be to entirely remove xstream from your pom, relying on the transitive dependency. Doing this will result in a build failure if the dependency to mylibrary is removed. This will act as a notification to you that you need to fix something. You won't silently loose required dependencies, and you won't silently have dependencies you no longer need.
On a side note, mvn dependency:analyze can be used to check for dependencies that are included but not used.
As mattb's answer says, declaring the dependency as test scope overrides the transitive compile-scoped dependency declaration, and as a result the dependency is not included in your packaged war.
If you only need the dependency in your tests because 'mylibrary' needs it to execute, you shouldn't declare the dependency at all in your project's pom. Let the transitive dependency resolution process handle it.
If your project does use the xstream jar directly, you can still rely on the transitive dependency, as you will need a compatible version for your project and 'mylibrary' to both run against the xstream jar. You should have unit tests that exercise the functionality, and if mylibrary changes version of xstream to an incompatible version, your builds should fail, and you can address the issue at that point.
In general I'd say you should try to avoid declaring dependency versions directly in multi-module projects. I declare the versions in a dependencyManagement section of a parent POM so that the child need only declare the groupId/artifactId. Alternatively, from Maven 2.0.9 onwards there is an additional dependency scope of import:
This scope is only used on a dependency of type pom in the section. It indicates that the specified POM should be replaced with the dependencies in that POM's section. Since they are replaced, dependencies with a scope of import do not actually participate in limiting the transitivity of a dependency.
So using import scope you can define your common dependency versions in a single POM, import the dependencies of that POM into your dependencyManagement section, and just declare the groupId/artifactId of the dependency in your other POMs.
By declaring your own dependency on xstream, and setting the scope to test, you are overriding the dependencies declared by mylibrary.
This is actually a Maven feature - it allows you to do things such as depend on a later version of a transitive dependency within your own project, and not end up packaging two different versions of the same artifact. For example, you might depend on version 1.2.15 of log4j, but because you also use libraryX which depends on log4j-1.2.14 - you wouldn't want both log4j-1.2.15 and log4j-1.2.14 to be packaged with your project.
If you actually want xstream to be packaged within your project, you should not be declaring the scope as test. In fact if you remove your listed dependency on xstream, things will work out as you like, since mylibrary has a compile dependency on it..
If you want it packaged, why are you declaring scope? If it is required at compile and execution time, shouldn't you leave the scope blank? If you did that, then you would only need
<dependency>
<groupId>mycompany.modules</groupId>
<artifactId>submodule</artifactId>
<version>1.0.1</version>
</dependency>
in your pom. Unless there is a reason to descope it during compile but not during packaging?