Hey I have created a Maven Project in IntelliJ and added some dependencies in my pom.xml for using external libraries. But I always have to import the classes in the class where I want to work with the classes of these libraries.
For example one dependency:
<dependency>
<groupId>org.datavec</groupId>
<artifactId>datavec-local</artifactId>
<version>${dl4j.version}</version>
</dependency>
And I use this line of code in my class:
DataAnalysis analysis = AnalyzeLocal.analyze(schema, recordReader);
HtmlAnalysis.createHtmlAnalysisFile(analysis, new File("X:/Churn_Modelling/analysis.html"));
I must import DataAnalysis.
I mean it would be ok if I would import the right class by clicking on import, but it is the wrong one.
You must use imports in your Java source code. Maven dependencies do not replace imports.
They make imports possible, though. Without the dependency, the import will fail.
If you can import the class then it means the dependency is successfully added to class path whether it is right or wrong.
Then the question is, is there only one DataAnalysis class in the class path. You can check by ctrl+space.
If it is the only one, then I think it is dependency problem. Your dependency version is dynamic,
${dl4j.version}
So in the build time may be it has different value. You can check it, IntelliJ maven tool Dependency part. In different dependency version, the class path would be changed.
If you are sure about, downloaded dependency with version is what you need, then it but the class is not what you expect, then try to restart the IntelliJ with clearing caches mode.
Hope one of these helps you.
Related
I'm working on a rect-native project and I'm trying to add a custom component to an already existing library. I want to add my own component to this library. I don't want to fork on the original project and create it there because my component will have different libraries and I think it will be harder to maintain a fork.
So I'm trying to import the project as a gradle dependency on a project of my own to import the original class AbstractMapFeature and extend its features to create my own class. I'm new to gradle and I don't even know if this is possible.
But if so, how do I import the project?
I tried through jipack:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
dependencies {
implementation 'com.github.rnmapbox:maps:+'
}
But got an error stating that it couldn't find the project. The project doesn't compile to jitpack as can be seen here.
I also tried to install it as a node dependency and import it local from there:
maven {
url "$rootDir/node_modules/#react-native-mapbox-gl/maps/android"
}
But I don't know how to import this into my code. Tried with:
import com.mapbox.rctmgl.components.mapview.RCTMGLMapView
Which is the same import statement they use, but got an error saying it couldn't find rctmgl.
So I have two questions. Is it really possible to import the original project's code and use it on my own? And if yes, how do I do this?
Jitpack can't successfully work on any Github repo. If you look at the instructions for that library, there is a specific open source fork of the library called MapLibre, which is what you'll need (assuming this is an Android project, since I otherwise don't see anything Java/Kotlin-specific). The library is published on mavenCentral() so you don't need Jitpack. You should specify the library as
implementation 'org.maplibre.gl:android-sdk:9.4.0'
If you're just creating a subclass of a class in the library, that's just normal usage of a library. If you need to actually swap out code that's in the library, you will have to fork it. If you're forking it, none of the above applies. You need to create a forked repo, make your changes, build it, and use the build from your fork as a dependency in your project.
My company is trying to get our team off Junit3 (and 4) and onto Junit5.
Our pom file has dependencies for Junit3, 4 and 5 already.
I'd like to somehow mark Junit3 and 4 as "deprecated at my company" to give anyone who is trying to use these older versions a visual clue in the UI that we want to stop using these.
Is there a method to mark some dependency in Maven as "deprected at my company?"
Short of that, I suppose we could write a checkstyle rule or some other static analysis, but I really think it would be less obtrusive to have the visual indicator (like javadocs or #Deprecated annotations).
Thoughts or addins that would do this?
If IDE matters, we have IntelliJ, Visual Studio code and a handful of stalwarts still on vim.
You can use the maven-enforcer-plugin to enforce not using those libraries: http://maven.apache.org/enforcer/enforcer-rules/bannedDependencies.html
You can have the plugin configuration shared across all projects (e.g. in the parent POM).
I'd like to find a utility that doesn't fail if any instance of a banned dependency is found, only new instances.
It looks like ArchUnit may give you the flexibility you are asking for. For example, a simple test below fails if any of the classes in the analyzed packages actually use classes from the org.junit package:
package com.stackoverflow.example;
import com.tngtech.archunit.junit.AnalyzeClasses;
import com.tngtech.archunit.junit.ArchTest;
import com.tngtech.archunit.lang.ArchRule;
import com.tngtech.archunit.lang.syntax.ArchRuleDefinition;
#AnalyzeClasses(
packages = "<root package of your module>"
)
public class JUnit4UsageTest {
#ArchTest
public static final ArchRule RULE_NO_CLASSES_SHOULD_ACCESS_JUNIT4_PACKAGES =
ArchRuleDefinition.noClasses()
.should()
.dependOnClassesThat()
.resideInAPackage("org.junit");
}
The DSL provided by the ArchUnit is powerful enough to express the rules, as long as the information is preserved in the class file.
The library also provides support for the freezing arch rules - they succeed for known recorded issues, but fail for any new one.
I have a Maven dependency, pulsar-log4j2-appender, which I forked and changed the source code because it was throwing exceptions in my project.
After changing the source code, I ran the maven package command to build the jar and imported it into my project (in Intellij: Project Structure | Modules | Dependencies | Add JARs or directories...).
However, when I run the application, it seems like it's not able to find that dependency because the Pulsar appender which I declared in my log4j2.xml file isn't being configured.
Am I importing the JAR properly? I'm wondering if the JAR needs to be within the org.apache.pulsar namespace to be imported properly.
For example,
This is what the dependency looks like unaltered:
And this is what it looks like when I modify and build it myself:
If you modify the code from an open source project you should change both the groupId and the artifact id. If you do not you will have problems and future developers will say your name in ways you do not want to hear.
Changing these are necessary so that Maven knows to use your version instead of the publicly available version. Also, when people look at your project and see the groupId and artifactId from the "real" project they will naturally assume that is what is being used (which is why they will curse you if that is not the case). In addition, you will have to do something convoluted to get Maven to use your dependency reliably.
The practice I have followed is the prepend "com.mycorp", where mycorp is my employer's name, to the groupId and add mycorp into the artifactId. The only downside to this is that you must ensure that the "real" artifact's coordinates are not referenced as a dependency or as a transitive dependency or else you will have duplicate classes on the class path.
Finally, your best bet is to create a pull request for Apache Pulsar with your fix so that other people experiencing the same problem you are get the benefit of it.
I added dependency to my pom file. I see that it was added to the dependency tree. In the class Idea proposes to import the class I need but when I press alt+enter nothing happens, it just proposes to import again.
I tried all the solutions mentioned here: Intellij Cannot resolve symbol on import
(invalidate cache, maven reimport, checked source and dependency folders, everything). Nothing helped.
I use Intellij Idea 2019.2 Ultimate edition.
i have a maven multi-module project:
root
commons
common-module
plugins
plugin
commons and plugins don't have the same parent, and are "standalone".
in commons-module i define 2 profiles, projectA and projectB. in each of these profiles i define properties, such as dep.version, dep1.version etc...
later i use these properties in dependencyManagemnt for the version part in the dependencies of "dep" and "dep1" section.
in plugins (who is parent for plugin) i have a dependency scope import on commons-module to obtain the list of dependencies.
when i build the plugin module, it doesn't seem to matter if i do -PprojectA or -PprojectB: maven says they don't exist. they do exist, but in commons module, which i import.
so the dependencies i get don't have the correct versions when i mvn dependency:tree
is what i'm trying to achieve possible, am i don't something wrong, is this a maven bug, or a none-supported feature? anyone got a clue?
thanks,
Nathan.
Import scope only imports the dependencyManagement, not the dependencies themselves. You still need to declare the dependency on the artifact, and then the version, and scope etc will be picked up from the dependencyManagement.
I don't think import will work for pluginManagment sections. The documentation you linked to only mentions dependencyManagement, and the only other mention I've seen is this unanswered question to the mailing list.
Dependencies aren't allowed to change the POM of modules that simply use those dependencies. It wouldn't be a safe thing to do. Imagine that you're adding another dependency to your project, and suddenly the build stops working because the dependency actually overrides some of your settings.
POM interpolation inherits settings only from ancestor projects, going up the <parent> chain.
You are trying to use the import scope with profiles. However, profiles are not activated transitively, so the different dependencies in your import-scoped dependency's POM aren't being activated.
It's probably not recommended, but you could have two different commons modules, and include the import-scoped dependency in profiles that refer to one or the other in your current project.