I'm learning Spring from this tutorial:
http://courses.caveofprogramming.com/courses/the-java-spring-tutorial/lectures/38024
In this tutorial, instructor downloads spring dependencies (spring-beans, spring context, spring-core) in version 3.2.3.RELEASE.
and then writes this code:
package com.caveofprogramming.spring.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class App {
public static void main(String[] args) {
ApplicationContext context = new FileSystemXmlApplicationContext("beans.xml");
Person person = (Person)context.getBean("person");
person.speak();
}
}
When I use: spring-context, spring-beans and spring-core in last version 4.3.3.RELEASE then ApplicationContext import doesn't work. It works when I change it to the old version. "Doesn't work" means that eclips doesn't know what I should import when I write "ApplicationContext context = new FileSystemXmlApplicationContext("beans.xml");" and when I write "import org.springframework.context.ApplicationContext" by myself it's underline.
What should I do to import ApplicationContext with newest version dependencies?
Edit:
This is the error:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
ApplicationContext cannot be resolved to a type
FileSystemXmlApplicationContext cannot be resolved to a type
at com.caveofprogramming.spring.test.App.main(App.java:10)
and this is my pom file:
<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.caveofprogramming.spring.test</groupId>
<artifactId>spring-tutorial-5</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.3.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.3.RELEASE</version>
</dependency>
</dependencies>
</project>
Edit 2: I also saw that program accepts 4.1.1.RELEASE version. Mabye the newest version of dependencies isn't necessary? I'm just starting with Spring and everyone says that I should work on the newest version.
Edit 3;
The only solution which I found is using spring-context 4.1.1.RELEASE
Either add these jars in your class path org.springframework.context.support-3.1.0.RELEASE.jar and org.springframework.context-3.1.0.RELEASE.jar
Or add this dependency if you are using maven and update the project.
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.x.x.RELEASE</version>
</dependency>
You have to add dependency
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.x.x</version>
</dependency>
This might be a problem with eclipse, especially if you are using an older version. Eclipse used to have issues with Maven projects, when you added new dependencies.
You can force Eclipse to update it's Maven dependencies by right clicking your project and selecting Maven->Update project
After that your project should compile just fine:
PS: Check the current documentation for up-to-date setup of XML-based application context setup
I added this in my pom.xml file inside the dependencies tag.
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>5.3.7</version>
<type>pom</type>
</dependency>
What it does is that it downloads the spring-context-5.3.7.jar file from which ApplicationContext class can be imported.
Hope it works for u too.
Here is my pom.xml file for better reference.
<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.Sharma</groupId>
<artifactId>NachoVarga</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>NachoVarga</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>
<!-- https://mvnrepository.com/artifact/org.springframework/spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.7</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>5.3.7</version>
<type>pom</type>
</dependency>
</dependencies>
</project>
Related
I have maven project with spring dependency successfully added. Component annotation is resolved when used, but Autowired annotation generates error, despite that it appears on the build path. How do i fix this?
first component class
import org.springframework.stereotype.Component;
#Component
public class Comp1 {
}
second component class
import org.springframework.stereotype.Component;
//error: The import org.springframework.beans.factory.annotation.Autowired cannot be resolved
import org.springframework.beans.factory.annotation.Autowired;
#Component
public class Comp2 {
#Autowired // error: Autowired cannot be resolved to a type
private Comp1 comp1;
public Comp1 getComp1() {
return comp1;
}
}
pom.xml
<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>Spr</groupId>
<artifactId>Spr</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
</dependencies>
</project>
Add these dependencies
</dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
Add Spring beans dependency, since Autowire annotation is part of spring-beans dependencies:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>your-version</version>
</dependency>
Deletion of local maven repository folder (.m2) and redownloading it, solved the problem.
I'm developing a Maven plugin and I'm getting the error: No mojo definitions found for plugin: my.plugin:my-artifact-id but ONLY if I have <packaging>maven-plugin</packaging> added to my pom. If I comment it out or remove it my plugin compiles just fine.
Here's what my project looks like so far:
Structure:
- pom.xml
- src
- main
- java
- my.plugin
- MyMojo.java
- // some other classes
In MyMojo.java:
#Mojo(name = "mygoal")
public class MyMojo extends AbstractMojo {
#Parameter
private String someParameter;
public void execute() throws MojoExecutionException {
//code here
}
// other methods
}
In my pom:
<?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">
<parent>
<artifactId>my-artifact-id-parent</artifactId>
<groupId>my.plugin</groupId>
<version>0.7-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>my-artifactid</artifactId>
<name>My Plugin Name</name>
<packaging>maven-plugin</packaging>
<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>3.3.9</version>
</dependency>
<dependency>
<groupId>org.apache.maven.plugin-tools</groupId>
<artifactId>maven-plugin-annotations</artifactId>
<version>3.4</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.2.2</version>
</dependency>
</dependencies>
</project>
As far as I understand, it is necessary for this to be included in order for me to import this plugin as a dependency of another project. Is that so?
I have tried using skipErrorNoDescriptorsFound, as described in other posts. This allows my plugin to compile, but when I try to import it into another project and use it, the other project cannot find the goal mygoal.
I would appreciate any guidance on this issue. Thanks.
IntelliJ does not let me import com.microsoft even though Maven resolves the dependency and it appears as a Module in my project settings (thus should be in the classpath).
External dependency resolves:
Modules:
POM file has this:
<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.arcologydesigns.rest</groupId>
<artifactId>ads-webapp</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>springexample Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
<spring.version>4.0.5.RELEASE</spring.version>
<jdk.version>1.8</jdk.version>
<jackson.version>2.2.3</jackson.version>
<!--<sqlserver.version>4.2.6420.100</sqlserver.version>-->
<sqlserver.version>4</sqlserver.version>
<build.scm.revision>SCM_IDENTIFIER</build.scm.revision>
<build.number>BUILD_NUMBER</build.number>
<build.id>BUILD_ID</build.id>
<build.job.name>BUILD_JOB_NAME</build.job.name>
</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-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>sqljdbc4</artifactId>
<version>${sqlserver.version}</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20120521</version>
</dependency>
</dependencies>
<build>
<finalName>ads-webapp</finalName>
</build>
</project>
I used the following to install sqljdbc driver (which I downloaded from MSDN) for Maven: http://techmajik.com/2014/04/24/how-to-setup-maven-dependency-for-microsoft-sql-server/
Try adding it directtly to Maven repository like this:
mvn install:install-file -Dfile=sqljdbc4.jar -Dpackaging=jar -DgroupId=com.microsoft.sqlserver -DartifactId=sqljdbc4 -Dversion=4.0
and then add this to your POM.xml
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>sqljdbc4</artifactId>
<version>4.0</version>
for more detailes you can read this topic:
http://claude.betancourt.us/add-microsoft-sql-jdbc-driver-to-maven/
You need to do this because SQL Server driver is not included in maven repository - and you need to install it yourself.
You can directly use this below dependency:
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>6.1.0.jre8</version> </dependency>
The older sqljdbc driver (v 4.0) jar file is available at the maven repo :
https://mvnrepository.com/artifact/com.microsoft.sqlserver/sqljdbc4/4.0
Download and install it using the command as mentioned by user4198625 taking care to
make sure that maven (mvn) in in your PATH
JAVA_HOME is correctly defined in your env variables (for Windows). This is expecially important if you are running the mvn batch file.
For the command to work as is, you need to execute it from the directory where the jar file is present.
i'm following a tutorial to understand how cassandra works,
but i have a problem with an import
import static com.datastax.spark.connector.CassandraJavaUtil.*;
this import is not recognized, especially this row:
javaFunctions(productsRDD, Product.class).saveToCassandra("java_api", "products");
my pom.xml is this:
<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.sparkexamples</groupId>
<artifactId>cassandraExample</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>cassandraExample</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>com.datastax.spark</groupId>
<artifactId>spark-cassandra-connector_2.10</artifactId>
<version>1.3.0-M1</version>
</dependency>
<dependency>
<groupId>com.datastax.spark</groupId>
<artifactId>spark-cassandra-connector-java_2.10</artifactId>
<version>1.3.0-M1</version>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-core_2.10</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-streaming_2.10</artifactId>
<version>1.3.1</version>
</dependency>
</dependencies>
</project>
how i can fix it?
thanks in advance.
Use this import, you are following the old API.
import static com.datastax.spark.connector.japi.CassandraJavaUtil.javaFunctions;
Check your /.m2/ directory for the Spark connector jar file. If it's not present, do a mvn -U which will force update the dependencies. If it's there, delete that directory from .m2 (only the Spark connector directory) and do a mvn clean compile> which should download the jar file again.
If you're working with Eclipse and not CLI, then you can right click on the project in Eclipse -> Maven -> Update project. Not sure about how it works in Netbeans/IntelliJ but there must be similar options there as well.
Also please check if your mvn can connect to the internet from your system. I hope this is not the case but there could be a chance the your mvn could be working offline.
Please check your pom:
<dependency>
<groupId>com.datastax.spark</groupId>
<artifactId>spark-cassandra-connector-java_2.10</artifactId>
<version>1.3.0-M1</version>
</dependency>
and
<dependency>
<groupId>com.datastax.spark</groupId>
<artifactId>spark-cassandra-connector_2.10</artifactId>
<version>1.3.0-M1</version>
</dependency>
Please check if this is causing duplicates and there is a conflicting jar.
I'm working on a multi-module maven project called acme-platform, with the modules set up like so:
acme-admin
acme-common
acme-global
acme-services
acme-client
acme-registration
acme-properties
acme-test
(They are listed in this order in the acme-platform pom.)
In some of the modules, I have been able to use Spring's ReflectionTestUtils class. However, in the last module, acme-test, where I really want to use it, I am unable to. There was no dependency section in the acme-test pom, so I added one. Here is the pom:
<?xml version="1.0" encoding="UTF-8"?>
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>acme-platform</artifactId>
<groupId>com.awesomeness.acme</groupId>
<version>1.21.0</version>
<relativePath>../</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>acme-test</artifactId>
<version>1.21.0</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
</dependencies>
Before adding the dependency lines, I couldn't import any of Spring's api into my classes. After importing these lines, I was able to access most of the classes, but not all of them, and in particular not ReflectionTestUtils, even though it is part of the spring-test module (as can be verified here).
I am using Intellij. I have looked at answers to other questions (such as this one) to make sure I'm updating my dependencies correctly. To no avail.
Does anyone have any idea as to why I can't import org.springframework.test.util.ReflectionTestUtils into acme-test?
Let me know if you need any aditional information.
EDIT
The version information of the dependencies are not in any of the module poms, but they are specified in the root pom (acme-platform). Again, I can import ReflectionTest in the other modules, just not in acme-test. So I deduce from this that as long as the dependency is declared with a specified version in the root pom, it doesn't need a version specified in any of the module poms. (If I'm wrong on this, please correct me.)
ADDITIONAL EDIT
By the way, I can't import junit either.
You need to set the Maven scope to test for both the spring-test and junit dependencies.
For example:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>