I am new to Dagger 2 and am trying out the Dagger 2 Coffee Example in IntelliJ and it seems that is does not generate a DaggerCoffeeApp_Coffee (It should be generating it) even though I followed the code example for Dagger 2 in github closely.
Public class CoffeeApp {
#Singleton
#Component(modules = {DripCoffeeModule.class})
public interface Coffee {
CoffeeMaker maker();
}
public static void main(String args[]){
Coffee coffee = DaggerCoffeeApp_Coffee.builder().build();
}
}
Here is my pom.xml file.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.tim.test</groupId>
<artifactId>Dagger2Experiment</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<slf4j-api.version>1.7.12</slf4j-api.version>
</properties>
<repositories>
<repository>
<id>sonatype</id>
<name>sonatype-repo</name>
<url>https://oss.sonatype.org/content/repositories/snapshots/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.google.dagger</groupId>
<artifactId>dagger</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>com.google.dagger</groupId>
<artifactId>dagger-compiler</artifactId>
<version>2.0.1</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j-api.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j-api.version}</version>
<scope>runtime</scope>
</dependency>
</dependencies>
</project>
I have tried various solution from the topic below but nothing seems to work:
I have also added jar file to my application buildpath dagger-2.0.1.jar in your application's runtime and dagger-compiler-2.0.1.jar in your build at compile time.
Update
I used DaggerCoffeeApp_Coffee.builder().build() in the code snippet above since I edited my code to follow the code example in Dagger 2's github below after I couldn't find the constructor. Link below:
https://github.com/google/dagger/blob/master/examples/simple/src/main/java/coffee/CoffeeApp.java
Any help will be greatly appreciated.
Update
Yep, You caught me skimming the question. I failed to read your code example otherwise I would have seen the pom.xml wasn't the only issue.
I'm assuming your DripCoffeeModule is annotated correctly and has no parameters in its constructor so that you don't need to specify this in building the Component. E.g. :
#Module
public DripCoffeeModule {
//Uses default constructor
}
I've not seen the Component implemented as an inner class before but I'm betting that Dagger won't treat this any different in terms of instantiating. (I would recommend moving it out of CoffeeApp class.) The naming, however, would be incorrect. Instead of
DaggerCoffeeApp_Coffee.builder().build();
You have to follow the naming convention defined by Dagger. From the section titled Building the Graph on Dagger's website:
The implementation has the same name as the interface prefixed with Dagger.
So you need to modify the line to:
DaggerCoffee.builder().build();
or you can use the convenience method:
DaggerCoffee.create();
If I'm wrong about number 1, then you will need to construct your Module as well like so:
DaggerCoffee.builder().dripCoffeeModule(new DripCoffeeModule()).build();
Original
Move the compiler from the dependencies section into the compiler section. From the Dagger 2 official site.
In a Maven project, one would include the runtime in the dependencies section of your pom.xml, and the dagger-compiler artifact as a dependency of the compiler plugin:
For example:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<dependencies>
<dependency>
<groupId>com.google.dagger</groupId>
<artifactId>dagger-compiler</artifactId>
<version>2.0</version>
<optional>true</optional>
</dependency>
</dependencies>
</plugin>
I chased a similar problem then realized I was not importing the #Component interface generated implementation. Once I added the import, using the same package path as my interface, my code compiled and worked.
Related
I am trying to integrate JavaFX inside of an SWT application using FXCanvas. For reference, I am following this oracle guide
Within my IDE this chunk of code displays an error
/* Create an FXCanvas */
final FXCanvas fxCanvas = new FXCanvas(shell, SWT.NONE) {
#Override
public Point computeSize(int wHint, int hHint, boolean changed) {
getScene().getWindow().sizeToScene();
int width = (int) getScene().getWidth();
int height = (int) getScene().getHeight();
return new Point(width, height);
}
};
Error:
(yes, I have imported the correct Point class: org.eclipse.swt.graphics.Point):
'computeSize(int, int, boolean)' in 'Anonymous class derived from javafx.embed.swt.FXCanvas' clashes with 'computeSize(int, int, boolean)' in 'javafx.embed.swt.FXCanvas'; attempting to use incompatible return type
However, I don't think this is the root cause... Because when I try to build the project (maven) I get this error:
package javafx.embed.swt does not exist. Which I believe is the true issue.
So after doing some research I have checked a few things, firstly, the jfxswt jar looks like it should be accessible:
and if I open the JAR, you can see FXCanvas is there:
I even tried adding the JAR manually as a library to my module, still doesn't work.
Here is my pom.xml, (i've intentionally anonymized certain info)
I will also mention that I have tried this in a fresh project without any maven dependencies, and just adding swt and such as libraries manually with the same error.
<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId></groupId>
<artifactId></artifactId>
<version></version>
</parent>
<artifactId></artifactId>
<version>2.0.0.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name></name>
<description></description>
<dependencies>
<dependency>
<groupId>pentaho-kettle</groupId>
<artifactId>kettle-engine</artifactId>
</dependency>
<dependency>
<groupId>pentaho-kettle</groupId>
<artifactId>kettle-core</artifactId>
</dependency>
<dependency>
<groupId>pentaho-kettle</groupId>
<artifactId>kettle-ui-swt</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.swt</groupId>
<artifactId>org.eclipse.swt.gtk.linux.x86_64</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse</groupId>
<artifactId>jface</artifactId>
<version>3.3.0-I20070606-0010</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
</dependencies>
</project>
Am I missing something, any ideas?
You will need to add the jfxswt.jar file to your classpath for compile and for execution.
This can be done by using the system scope as that jar file is part of your JDK under the jre/lib directory.
<dependency>
<!-- GroupId/ArtifactId/Version doesn't matter unless it clashes. -->
<groupId>jfxswt</groupId>
<artifactId>jfxswt</artifactId>
<version>1.8</version>
<scope>system</scope>
<systemPath>${java.home}/lib/jfxswt.jar</systemPath>
</dependency>
This will instruct maven to add the jre/lib/jfxswt.jar to the classpath. This could cause an issue if someone uses different JDK what has that jar in other places but for Java 8 you should be okay.
You have to add the jfxswt.jar to your classpath when you execute your application.
In maven you can use the exec plugin:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>twobuttons.TwoButtons</mainClass>
<additionalClasspathElements>
<!-- The run environment must configure the system jar. -->
<element>${java.home}/lib/jfxswt.jar</element>
</additionalClasspathElements>
</configuration>
</plugin>
Notes:
The system scope is deprecated in maven in favor of installing files to repositories. The solution above works fine at the moment.
The content of the jfxswt.jar can be part of some SWT libraries unfortunately I am not familiar with SWT. If you can find that jar in a Maven repo than just include that instead of messing with the system scope.
The twobuttons.TwoButtons class is from the tutorial you mentioned.
I am trying to build a Java 11 project with maven and lombok's #Slf4j Logger, but maven does not recognize the log variables. IntelliJ does though and is able to build the project.
The error is
[ERROR]: cannot find symbol variable log
Project and Module SDK is both Java 11. Lombok Version is 1.18.2:
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
<scope>provided</scope>
</dependency>
My maven compiler setup:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>11</source>
<target>11</target>
<forceJavacCompilerUse>true</forceJavacCompilerUse>
<annotationProcessorPaths>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>
I already tried:
turning Annotaion Processing off and on again
reinstalling Lombok plugin
clearing .m2/repository folder
manually adding lombok.jar as Annotation Processor
adding Lombok path to maven-compiler-plugin list of Annotation Processor
This is a really minimal example configuration for using the #Slf4j lombok logging annotation.
You need a logging facade and an implementation, in this case I'm going to use slf4j (as facade) and logback (as implementation).
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>untitled</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
<scope>provided</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.30</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
</dependencies>
</project>
main.java
import lombok.extern.slf4j.Slf4j;
#Slf4j
public class Main {
public static void main(String[] args) {
log.debug("Hello");
}
}
If you get some trouble try always to force the maven dependencies updates running in your project folder mvn -U clean package and reimporting maven project in your IDE
My suspicion is that this is a misleading error message as a consequence of the point that Lombok hooks in during compilation.
In bytecode, there is no concept of an import. Classes are replaced by their fully qualified names (e.g. Integer to java.lang.Integer). Therefore at some point in compilation, the imports are parsed, applied, and any unknown classes (e.g. due to lack of the correct dependency) will throw an error at this stage.
Since #Slf4j means you do not need to import org.slf4j.Logger, the step described above is missed for this class.
After Lombok has appended the log field, the compiler must subsequently look at it's usage, see the class org.slf4j.Logger which it does not recognise and throws an error. Under normal circumstances, due to the earlier compilation stage, the only possible cause is that the field doesn't exist, so infers that the symbol log must be missing. What it is really failing to understand is the type of the field log.
Because Lombok makes changes in the middle of compilation, I guess spurious errors such as these are always a possibility. Perhaps Lombok developers could fix it by doing their own check for org.slf4j.Logger. Most of the functionality provided by Lombok does not involve "importing" external classes, so I'm not surprised that it doesn't handle this edge case as elegantly as possible.
If you add the dependency for SLF4J, the compiler will no longer complain.
In lombok.config file for lombok.log.custom.declaration property use full name including the package for the classes
lombok.log.custom.declaration=com.mycomp.logging.Log
com.mycomp.logging.LogFactory.getLog(TYPE)(TOPIC)
i have made two maven projects : the first is a generic authentification module with spring security with this structure :
ear
|...warModule
| |...ejbModule
|...ejbModule
the second is a CRM have this structure
ear
|...warModule
|...ejbModule
|...ejbModule
now i want to integrate both so i can manage the CRM security with my authentification project (control url access ,permissions ...)is there a way to do that ?
One way is that, you declare first project as dependency in your second project POM file.
For detail you can check: Maven Dependency Mechanism
Sample code to add Dependencies:
<project>
...
<dependencies>
<dependency>
<groupId>group-a</groupId>
<artifactId>artifact 1</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>group-a</groupId>
<artifactId>artifact 2</artifactId>
<version>1.0</version>
<scope>runtime</scope>
</dependency>
</dependencies>
</project>`
Second way and I would suggest to use Overlay approach to integrate multiple modules and another benefit to use overlay is can share common resources across multiple applications.
You have just to add a dependency declaration in one pom.xml file which point to the other artifact. For example lets say the downside lines are your CRM pom file:
<project>
<groupId>crm.group.id</groupId>
<artifactId>crm-artifact-id</artifactId>
<version>1.0</version>
<packaging>ear</packaging>
<dependencies>
<dependency>
<groupId>authentication.group.id</groupId>
<artifactId>authentication-artifact-id</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
</project>
It is just a sample and should be updated following your projects/modules group/artifact IDs.
Is there a way to add a pom type dependency to my POM and get all its modules?
JavaMail is a good example. Maven Central Repo has a parent POM called: com.sun.mail:all:1.5.0 with modules: mail, mailapi, mailapijar, smtp, imap, gimap, pop3, and dsn.
However, the "all" artefact only has a single file: pom.xml Is there a way to add this "all" artefact as a dependency to my POM and get all its modules? I am 90% sure this is not the right way to use dependencies in Maven, but I want to hear it from an expert on The Stack.
Ideas:
<dependencies><dependency>...<type>pom</type></dependency></dependencies>
<dependencyManagement><dependencies><dependency>...<type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement>
Related: Netbeans: maven dependencies of type pom
You have to go with
<dependencies>
<dependency>
<groupId>com.my</groupId>
<artifactId>commons-deps</artifactId>
<type>pom</type>
</dependency>
</dependencies>
This will transitively add all dependencies declared in com.my:commons-deps to your current POM.
Using
<dependencyManagement>
<dependencies>
<dependency>
<groupId>...</groupId>
<artifactId>...</artifactId>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
works as a simple 'include' of artifacts versions in your dependency management. Thus, it won't add any dependency in your project.
I believe you can create your own POM which aggregates the dependencies you want, and then in your original project add a dependency on that aggregate pom. You will still have to add dependencies on each individual module in your dependency POM, but it will be abstracted from the actual project POMs and allows those dependencies to be managed in one place, which could become useful if you end up having multiple projects that depend on that set of dependencies.
In your example you could create a new pom like this:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany</groupId>
<artifactId>mail-deps</artifactId>
<version>1.0.0</version>
<packaging>pom</packaging>
<dependencies>
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>mail</artifactId>
<version>1.5.0</version>
</dependency>
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>mailapi</artifactId>
<version>1.5.0</version>
</dependency>
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>mailapijar</artifactId>
<version>1.5.0</version>
</dependency>
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>imap</artifactId>
<version>1.5.0</version>
</dependency>
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>gimap</artifactId>
<version>1.5.0</version>
</dependency>
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>pop3</artifactId>
<version>1.5.0</version>
</dependency>
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>dsn</artifactId>
<version>1.5.0</version>
</dependency>
</dependencies>
</project>
Then in your original project just add:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">
...
<modules>
<module>src/main/java/com/mycompany</module>
</modules>
...
<dependencies>
<dependency>
<groupId>com.mycompany</groupId>
<artifactId>mail-deps</artifactId>
<version>1.0.0</version>
<type>pom</type>
</dependency>
</dependencies>
</project>
The short answer: You cannot do this in Maven.
The other answers make only the "all" POM a dependency. Does not solve the issue. Another answer tries to import the dependencies of the "all" POM. I don't need the dependencies; I need the (child) modules of the "all" POM. Again, does not solve the issue.
Side note: I was using the JavaMail library incorrectly. I only needed to add one dependency: com.sun.mail:javax.mail:1.5.0
If the pom you're trying to import, contains dependencies defined in a <dependencies/> section, and you would like to import them all, you can try the code below.
(Disclaimer: I haven't done this in a while): in your <dependencyManagement/> section, add the pom dependency like this:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>kung.fu<groupId>
<artifactId>ninja</artifactId>
<version>1.2.3</version>
<scope>import</scope>
<type>pom</type> <!-- Not too sure if you needed this
when it's scoped as import,
but just in case -->
</dependency>
</dependencies>
</dependencyManagement>
It may as well be the case that you define the dependency directly in the <dependencies/> section not needing the <dependencyManagement/> bit, but as far as I recall, it should be scoped import as shown above.
As someone already wrote above : You can't do it . But this is what i did and it worked .
Lets assume you have some pom file (JavaMail in your example) with following :
<type>pom</type>
<dependencyManagement><dependencies><dependency></dependencyManagement>
And You want copy all jars mentioned in this pom to some place .
This is what i did and it is fast working solution
Open original pom and just copy-paste all dependencies section from original pom file to your new pom as is .
Of course use maven dependency plugin to copy all .
I need some help with setting up a spring project.
I am busy going through the book “Spring in action” and I need to try some of the examples out. I have looked at plenty of pages and nowhere can is see where I am going wrong. It must be something silly that I missed or overlooked.
I installed Spring source tool suite
Created a new java/maven project
Added a new applicationContext.xml bean definition file
The outline of the project looks as follows
I created my beans (vwCar & nissanCar that implements car interface) and where it comes to use them I have a main method in the app class. I need to create an application context.
ApplicationContext context = new ClassPathApplicationContext("src/main/resources/applicationContext.xml");
But I have difficulty creating the ApplicationContext. It gives me an error and code assist don’t work
Using code assist the only thing that it suggests is (Pressing Ctrl+Space after typing app):
If I just type it out I get an error
ApplicationContext cannot be resolved to a type in class App.java
Is there something that I should import myself?
I can see an "S" on the project folder - doesn't this indicate that the project is already spring enabled?
---------ADDED AFTER ALEX COMMENTED TO SUGGEST THAT I SHOULD ADD A MAVEN DEPENDENCY-------------
I added the missing dependency like Alex suggested but I don't know what the correct version is. If I look a the STS directory I see several files named ... 2.9.2
org.springframework.ide.eclipse.feature_2.9.2.201205070117-RELEASE
but if I add the dependency with 2.9.2 i get the following error on my POM
Missing artifact org.springframework:spring-context:jar:2.9.2
My POM looks like below
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>my.chrispie</groupId>
<artifactId>MyMavenSpringProject</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>MyMavenSpringProject</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>2.9.2</version>
</dependency>
</dependencies>
</project>
Since you're using Maven you should add the following dependency to your pom.xml:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${org.springframework-version}</version>
</dependency>
Where ${org.springframework-version} should be replaced with the version you are using.
This will ensure that the Spring jars needed to get started are available to your application.
It must be something silly that I missed or overlooked.
I think you'v just overlooked the dependency management functionality of Maven. Creating a Java/Maven project doesn't pull in the required Spring jars. If you used a template project from the STS landing page this would all have been setup for.