Maven Exec plugin, ClassLoader ClassNotFoundException - eclipse

I've tried many suggestions on StackOverflow, and tested with many combinations of setup and pom.xml for 3 days, none of them works. Please help.
I started with a big project with a lot of dependencies, spring-boot, hibernate, etc. Then I create another small console project which import and use some classes from the big project. They are not parent and child project. All I do is add <dependency/> to the child project pom, like this.
P.S. the big project has a <parent/> spring-boot-starter-parent and use spring-boot-maven-plugin.
<dependency>
<groupId>myGroup</groupId>
<artifactId>bigProject</artifactId>
<version>1.0.1</version>
</dependency>
This works on Eclipse with m2e, I just use "Run as" java application and the small project works.
But I need to upload and run those project on my linux VM, which does not use GUI and Eclipse, only terminal.
Then after some reading, I try to use maven exec plugin to run the small project. My steps:
1. do mvn clean install on the big project, confirmed that it appears in my /.m2, local repository.
2. run mvn compile on small project
3. run mvn exec:java on small project
It fails on step 2, those import ... in the class of small project throw package xxx does not exist. maven fail with compilation error.
Then, I try to simplify the problem. I create two test projects with only 1 class, myLib and HelloWorld console application, then I add myLib dependency (pom). HelloWorld project to print a message from the class in myLib package. run step 1 to 3. It works.
public class App
{
public static void main( String[] args )
{
//SystemLog from big project, does not work
//SystemLog log = new SystemLog();
//log.setValue("test value");
//System.out.println(log.getValue());
//CustomMessage from myLib, works fine
CustomMessage cm = new CustomMessage();
cm.setTheMessage("custom message");
System.out.println(cm.getTheMessage() );
System.out.println(CustomMessage.defaultMsg);
}
}
pom.xml of small project
<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>test</groupId>
<artifactId>dependOnOthers</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>dependOnOthers</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>test</groupId>
<artifactId>myLibrary</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com</groupId>
<artifactId>bigproject</artifactId>
<version>1.0.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.5.0</version>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<includePluginDependencies>true</includePluginDependencies>
<mainClass>test.dependOnOthers.App</mainClass>
<!-- <arguments> <argument>argument1</argument> </arguments> -->
<!-- <arguments> <argument>-classpath</argument> <argument>target/lib/bigproject-1.0.1.jar</argument>
</arguments> -->
</configuration>
</plugin>
</plugins>
</build>
</project>
Then I add a reference to big project, try to print a log message using HelloWorld, it pass step 2, mvn compile, but when I do step 3, mvn exec:java, java.lang.ClassLoader.loadClass() throw ClassNotFoundException on the line where I new an SystemLog() instance, the class defined in big project.
Summarize:
Goal: try to run a console application using Maven exec plugin, which has a dependency on another project, that is installed in local repo /.m2
The SystemLog class is a model class with hibernate annotations.
1. small project depend on big project, fail on mvn compile, package does not exist.
2. HelloWorld depend on myLib, works fine.
3. HelloWorld depend on big project, fail at runtime with ClassNotFoundException.

It's a little late but i ran in the same error, migrating a project from 1.5.3 to 2.1.4
I have found a solution for this problem.
Spring Boot has changed the jar structure in version 2.0.0+, so you don't need to specify your class as <mainClass>test.dependOnOthers.App</mainClass>, you need to point JarLauncher as main entry point.
When the plugin will execute the jar, java will call the main method JarLauncher and several code of spring will be called after that he reads the manifest and call the class definied in Start-Class:. in your case test.dependOnOthers.App.
<configuration>
<!-- Since version 2 of spring, the jar is packed in
another way and has his main method here, don't worry in the MANIFEST file is described which file to boot -->
<mainClass>org.springframework.boot.loader.JarLauncher</mainClass>
The old strucutre of Spring 1.0, was the classical java way todo it, so the java classloader can load it without problems and refer to the class when it is in the classpath, with the version 2.0.0+, they copy user class files and dependency into BOOT-INF, so the java classloader cannot load it anymore, you need to use the Spring classloader to get the class instance.

I have a workaround and know what is causing the problem, but don't know all the details, hope that someone will comment on this, or elaborate the answer.
The thing that cause this problem is spring-boot-maven-plugin, which I use to run spring-boot embedded tomcat server, using mvn spring-boot:run. This plugin seems to change something on the .jar file, maybe the manifest?
The workaround is to remove this plugin, run mvn clean install, that .jar installed in local repository will work correctly. run mvn compile on the small project. Then add the plugin back to big project, run mvn spring-boot:run
I do not know what the plugin changes that result in a .jar with package not exist, does it changes package name?
My workaround is clumsy, if there is a way to choose to compile with or without spring-boot-maven-plugin by defining maven goal, so that I do not need to change the pom for different build (with/without plugin). That would be a better solution.

Related

Error "Source option 5 is no longer supported. Use 6 or later" on Maven compile

I am getting the following error on $ mvn compile:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project Sym360: Compilation failure: Compilation failure:
[ERROR] Source option 5 is no longer supported. Use 6 or later.
[ERROR] Target option 1.5 is no longer supported. Use 1.6 or later.
[ERROR] -> [Help 1]
Here is the code of my 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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test.app</groupId>
<artifactId>Tset</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>Test</name>
<url>http://maven.apache.org</url>
<properties>
<maven.compiler.source>6</maven.compiler.source>
<maven.compiler.target>1.6</maven.compiler.target>
</properties>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
</plugins>
</pluginManagement>
</build>
<dependencies>
<!-https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-
java -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.14.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.testng/testng -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.14.3</version>
<scope>test</scope>
</dependency>
I tried to add properties in the pom.xml code, but still getting the same error.
What helped me was these lines in pom.xml file
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
I had same issue, the problem is with properties. Check your JavaSE version in your project, it will be displayed beside JRE System Library folder in your project. If it is 1.5, then it will throw an error. Most probably you will have a updated version, so check the version and update it. I have updated it below based on your code.
<properties>
<maven.compiler.source>1.6</maven.compiler.source>
<maven.compiler.target>1.6</maven.compiler.target>
</properties>
Also in one of my IntelliJ IDEA projects, in addition to all of answers above, another try works:
Just change Language level in Modules section of Project Structure [image below]
I had same issue and i have added below configuration in pom.xml and it works.
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
I think you have wrong your pom.xml:
<properties>
<maven.compiler.source>6</maven.compiler.source>
<maven.compiler.target>1.6</maven.compiler.target>
</properties>
change to:
<properties>
<maven.compiler.source>1.6</maven.compiler.source>
<maven.compiler.target>1.6</maven.compiler.target>
</properties>
Now depending if you are using the command line use:
mvn clean compile
or either way(eclipse ide)
Right click on project Run with maven>build>Goal (compile)
In Eclipse This helped me:
Right Click on Project.
Click on Build path.
Click on Configure Build path.
It opens a Java Build path window.
Click on Java Compiler in the Left side.
It navigates to Java Compiler window in that to set the Compiler compliance level is set as according to your jre version(ex if java version is 1.8
then choose 1.8) as select.
Click on [Apply] button.
Click on [OK] button.
Right click on Project > Maven > Update the project.
Right click on Project > Run As > Maven install -- The pom.xml file is running and java jars are download and installed to project.
Right click on Project > Run As > Maven Test -- The pom.xml file is running and java jars are download and installed to project.
Then you got the Build Success message and your maven project is created successfully.
adding below code in pom will resolve the issue
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<profiles>
I was facing the same issue and resolved it with the lines of code below:
<properties>
<maven.compiler.source>1.6</maven.compiler.source>
<maven.compiler.target>1.6</maven.compiler.target>
</properties>
I fixed this by adding this in pom.xml file:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<failOnMissingWebXml>false</failOnMissingWebXml>
</properties>
For the new Apache net Bean its a little bit different from the suggestion by SUPARNA SOMAN
Right Click on your Project
-Hover on "set configuration" and click customize configuration
-.A new dialogue box opens....
At the left corner where the categories are, Click on "Source"
At the select form on the page below, select your required version of JDK ----see image for this last step.the last step required to change jdk version
On MacOS I have multiple versions
user> ls /Library/Java/JavaVirtualMachines/
jdk-11.0.4.jdk jdk-12.0.2.jdk jdk1.8.0_221.jdk
and JAVA_HOME was not defined properly so Maven used jdk-12. I have jdk-11,jdk-8, and jdk-12.
user> mvn -version
Apache Maven 3.6.1
Maven home: /usr/local/Cellar/maven/3.6.1/libexec
Java version: 12.0.2, vendor: Oracle Corporation, runtime: /Library/Java/JavaVirtualMachines/jdk-12.0.2.jdk/Contents/Home
Default locale: XXX, platform encoding: UTF-8
OS name: "mac os x", version: "10.14.6", arch: "x86_64", family: "mac"
So:
Define JAVA_HOME to use jdk-8.
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_221.jdk/Contents/Home
Try again, now maven is:
user> mvn -version
Maven home: /usr/local/Cellar/maven/3.6.1/libexec
Java version: 1.8.0_221, vendor: Oracle Corporation, runtime: /Library/Java/JavaVirtualMachines/jdk1.8.0_221.jdk/Contents/Home/jre
and the build is:
[INFO] BUILD SUCCESS
None of the solutions above worked for me.
For some reasons the file with the name of my project.iml had changed. Found the difference in the previous subversion repository submission...
In projectname.iml I found this line:
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_5">
And all I had to do was changing it to 11
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_11">
After so many years, happily seeing the same error ! :P
[ERROR] Source option 5 is no longer supported. Use 7 or later.
[ERROR] Target option 5 is no longer supported. Use 7 or later.
pom.xml did not have any source or target declarations, so maven must have taken some archaic versions as default, rather than being clever enough to get it from the install JVM (which is currently on 18).
So, without knowing the original issue, I could also resolve it through the below snippet below. That declares a specific source and target version, which subpresses the problem.
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>18</source>
<target>18</target>
</configuration>
</plugin>
</plugins>
</build>
Some people have asked:
I understand that this works, but I don't understand why maven uses a default source value that it does not support? –
The answer is:
Maven doesn't know what version of the JDK you will use.
When used with JDK 1.8 Maven compiles work successfully.
When using newer versions of the JDK, it's not clear what version of the byte code you want to use. So Maven's has punted and continues use what it has used for many years.
At this day and age, it is difficult for Maven (as a generic build tool) to select a default byte code version that everyone will like.
So it's probably best to get used to putting the version of your source code and the byte code you want to generate in your pom.xml file.
You can argue (and I would agree) that maven should (by default) use a newer version of the maven-compiler-plugin but as I stated, whatever version was picked, someone would have a problem with it.
Example
For example, if you use JDK 11, you might very well be using Java 11 syntax and need -source 11 -target 11 when compiling your code.
Even the most recent release of the plugin maven-compiler-plugin:3.10.1 defaults to JDK 1.7 syntax which would result in compilation errors for Java 11 code.
A full description of the problem
Other's have said this, but to be complete. The default pom.xml doesn't specify the maven-compiler-plugin. To find out the version used you can use
mvn help:effective-pom | more
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
You'll see that the maven-compiler-plugin:3.1 is being used.
You can see that this version generates a command with -target 1.5 -source 1.5 which generates an error when used with versions of Java newer than Java 1.8.
The following shows the error that occurs when using JDK 17.
mvn --version
Apache Maven 3.8.6 (84538c9988a25aec085021c365c560670ad80f63)
Maven home: D:\p\apache-maven-3.8.6
Java version: 17.0.2, vendor: Oracle Corporation, runtime: D:\p\jdk-17.0.2
Default locale: en_US, platform encoding: Cp1252
OS name: "windows 10", version: "10.0", arch: "amd64", family: "windows"
mvn clean install -X
. . .
[DEBUG] -d D:\Play\maven\helloworld\target\classes -classpath D:\Play\maven\helloworld\target\classes; -sourcepath D:\Play\maven\helloworld\src\main\java; -g -nowarn -target 1.5 -source 1.5
. . .
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] Source option 5 is no longer supported. Use 7 or later.
[ERROR] Target option 5 is no longer supported. Use 7 or later.
[INFO] 2 errors
The fix
The fix was to update the maven pom.xml file to specify either a newer maven-compiler-plugin. I tested with maven-compiler-plugin:3.10.1 and it uses -target 1.7 -source 1.7
The syntax is:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
</plugin>
</plugins>
</build>
At the time of this writing version 3.10.1 was the latest version.
The other option is to specify the version of the byte code you want to generate, one way is to use this (as stated in another answer):
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
This will work even with the old version 3.1 plugin.
Here is the solution which helped me:
I had the same issue on error source option 5 is no longer supported, Use 6 or later
So i followed these instructions and problem SOLVED
Open Project Properties (File Menu)
Change the Source / Binary Format to the latest version (JDK 7 in my case)
Project Properties
Source / Binary Format
Clean and Build, Then Run the project
This is a message from a newer javac, e.g.:
$ java -version
openjdk version "11" 2018-09-25
$ javac -source 1.5 -target 1.5 Test.java
error: Source option 5 is no longer supported. Use 6 or later.
error: Target option 1.5 is no longer supported. Use 1.6 or later.
So, apparently you're using a newer JDK version with a Maven version prior to 3.8.0 ("<source>/<target> ... NOTE: Since 3.8.0 the default value has changed from 1.5 to 1.6"). The maven-compiler-plugin:3.1 you use is from April 2013.
There are two possibilities:
Update your Maven version to the latest (I'd recommend that)
Setting the Java Version in Maven:
2.2. Java 9 and Beyond
...
<properties>
<maven.compiler.release>...</maven.compiler.release>
</properties>
If in Eclipse, Write click on project and go to properties. Search for maven and configure a jdk higher version (1.7 onwards) there and apply. Now try maven install.
In my case, running MacOS Big Sur and JDK version 15, I added the code below in pom.xml file shown below. I added the 15 for my JDK version.
<properties>
<maven.compiler.source>15</maven.compiler.source>
<maven.compiler.target>15</maven.compiler.target>
</properties>
I then re-ran $ mvn clean wildfly:deploy and it worked.
Both options work for me to resolved Source option 5 is no longer supported. Use 6 or later” on Maven compile
Open pom.xml file
Option1: add build tags
Option2: add properties tags
<project>
<groupId>com.pluralsight</groupId>
<artifactId>HelloWorld</artifactId>
<version>1.0-SNAPSHOT</version>
<modelVersion>4.0.0</modelVersion>
<packaging>jar</packaging>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<release>12</release>
</configuration>
</plugin>
</plugins>
</build>
</project>
OR
<project>
<groupId>com.pluralsight</groupId>
<artifactId>HelloWorld</artifactId>
<version>1.0-SNAPSHOT</version>
<modelVersion>4.0.0</modelVersion>
<packaging>jar</packaging>
<properties>
<maven.compiler.source>1.6</maven.compiler.source>
<maven.compiler.target>1.6</maven.compiler.target>
</properties>
</project>
I faced the same challenge, what I did was to go to:
File
Project structure
Module
Changed compile SDK to 30 (Latest at the time)
Build tools 30.0.3 (Latest)
Source compatibility (1.8 Java 8)
Target compatibility (1.8 Java 8)
Change the
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_5">
to
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_11">
Set updated java version from build path as per you installed
I solve the problem by finding that my $JAVA_HOME is empty. After setting it, compilation succeeded!

Spring MVC with Spring Boot doesn't work with the Tomcat server of Eclipse

I tried to follow Spring Getting Started Guide for "Serving Web Content with Spring MVC" which uses Spring Boot and Gradle in addition to Maven.
I installed Gradle plugins to Eclipse.
I want to run the application using the Tomcat server in Eclipse because of that I also followed "Converting a Spring Boot JAR Application to a WAR" guide and changed the "build.gradle" file as mentioned in the guide. Basically, I added lines "apply plugin: 'eclipse-wtp'", "apply plugin: 'war'", configurations {providedRuntime}, and providedRuntime("org.springframework.boot:spring-boot-starter-tomcat"); and changed jar settings to war settings. Here is the build.gradle file:
buildscript {
repositories {
maven { url "http://repo.spring.io/libs-milestone" }
mavenLocal()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.0.2.RELEASE")
}
}
apply plugin: 'java'
apply plugin: 'eclipse-wtp'
apply plugin: 'idea'
apply plugin: 'spring-boot'
apply plugin: 'war'
eclipse.project {
natures 'org.springsource.ide.eclipse.gradle.core.nature'
}
war {
baseName = 'gs-serving-web-content'
version = '0.1.0'
}
repositories {
mavenCentral()
maven { url "http://repo.spring.io/libs-milestone" }
}
configurations {
providedRuntime
}
dependencies {
compile("org.springframework.boot:spring-boot-starter-thymeleaf")
testCompile("junit:junit")
providedRuntime("org.springframework.boot:spring-boot-starter-tomcat")
}
task wrapper(type: Wrapper) {
gradleVersion = '1.11'
}
I also added HelloWebXml class as they mention. Here is the class:
package hello;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;
public class HelloWebXml extends SpringBootServletInitializer {
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
}
In addition to these I needed to change pom.xml a little because it was complaining about Java SE 7 features. Added the lines below to the pom.xml:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
<showWarnings>true</showWarnings>
<compilerVersion>1.7</compilerVersion>
<fork>true</fork>
</configuration>
</plugin>
The rest is the same as the getting started guide.
For building, I run
gradlew eclipseWtp
gradlew clean build
commands. After this the war file is created in build/libs folder of the project. If I copy the war file to my local Tomcat server and start the server, everything works as expected.
If I drag the project to the Tomcat server under the Servers tab (which is created using the same local Tomcat) and run the server, a ClassCastException is thrown with the complaint:
"Cannot cast org.springframework.web.SpringServletContainerInitializer to javax.servlet.ServletContainerInitializer".
I checked the folder structure of the project in both of the deployment locations.
In the local (non-Eclipse) deployment location, after the server starts a folder with the name of the war file is created as expected. In the WEB-INF folder, there is a lib-provided directory. I checked the deployment location of the Tomcat of Eclipse, it didn't include a directory named lib-provided. I guess the problem is about this directory not being created but I couldn't find a solution.
I was already using Spring MVC, and I know how to create MVC projects with web.xml but I am new to Spring Boot. The Tomcat server of Eclipse runs my previous Spring MVC projects fine. So the problem is about the Spring Boot project.
I checked several related questions but they were not the same one as mine. I couldn't find a solution to my problem.
What am I missing here?
Thanks in advance.
I imagine that you are getting a clash with the servlet-api JARs. If you are developing with embedded Tomcat (creating a JAR) you need to include the servlet-api JAR on your classpath in the compile scope. When you are deploying to an existing Tomcat installation (creating a WAR) you must declare the servlet-api jar in provided scope, otherwise it will end up in web-inf/lib and cause conflicts with the version provided by your container.
Here is the POM from the sample Spring Boot WAR project:
https://github.com/spring-projects/spring-boot/blob/master/spring-boot-samples/spring-boot-sample-traditional/pom.xml
Note how the spring-boot-starter-tomcat uses the provided scope.
Spring Boot does some special handling to allow executable WARs that run both embedded or in a container. Specific attention has to be payed to dependencies in provided scope since these might conflict with the container. The details are described in the Spring Boot reference guide. The reason your build works when deployed via a built WAR but not via Eclipse is that Eclipse WTP doesn't actually use Maven to perform the build, so a Maven build and a WTP build won't exactly match.
I'm working on addressing the same issue myself. What I plan to do is create a seperate 'dev' maven module specifically for this purpose, and use tags to switch (some of) the provided entries to runtime.
#meribald you can configure your pom.xml in that whay:
<profiles>
<profile>
<id>local</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<packaging-profile>jar</packaging-profile>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
</dependencies>
</profile>
<profile>
<id>prod</id>
<properties>
<packaging-profile>war</packaging-profile>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
</profile>
</profiles>
and on the top of your pom.xml
<packaging>${packaging-profile}</packaging>
So, when you are developing you run you application as a Java Application, and maven will generate a jar file. When you want to package a war file to deploy on a server, you run on a maven command line
mvn -Pprod package
or in Eclipse run as >> Maven Build ..., and put "package" on the goals field, and "prod" (without quotes) on profiles field.

File not found Maven

I am having some problems with simple cloud storage (simplecloud). When I run with Maven the console outputs following error :
java.io.FileNotFoundException: \var\key (The system cannot find the path specified)
Source code is here : src git
However it exists in the directory as shown here :
Any ideas?
Thanks in advance
POM :
<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.pliablematter</groupId>
<artifactId>simple-cloud-storage</artifactId>
<version>1.0-SNAPSHOT</version>
<name>Simple Cloud Storage</name>
<description>A simple wrapper around the Google Cloud Storage API</description>
<dependencies>
<dependency>
<groupId>com.google.http-client</groupId>
<artifactId>google-http-client-jackson2</artifactId>
<version>1.15.0-rc</version>
</dependency>
<dependency>
<groupId>com.google.apis</groupId>
<artifactId>google-api-services-storage</artifactId>
<version>v1beta2-rev6-1.15.0-rc</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>LATEST</version>
<scope>test</scope>
</dependency>
</dependencies>
Remove the initial \. Also, as pointed out by the others, as this is obviously a test key (judging by the name), you should place it under src/test/resources/var/key and change your code to look for just var/key/test.p12.
Make sure your \var\key directory is present in the target directory that Maven creates for the build. I'll go out on a limb and guess that it's probably not there now. If you have to have that \var\key in a non-standard place, you can use the Maven Resources Plugin to copy it into the target directory. As #Adrian Shum suggests, test\resources would be a standard place for Maven to look to find it.
EDIT:
Seems like you wanted to explore this option, so here's how you might use the Maven Resources Plugin in your Maven POM...
Inside the plugins node under build, add:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
</plugin>
Then after the plugins node again inside build, add the directory or directories you need to copy over to target, for example:
<resources>
<resource><directory>src/main/resources</directory></resource>
</resources>
<testResources>
<testResource><directory>src/var/key</directory></testResource>
<testResource><directory>src/test/resources</directory></testResource>
</testResources>
I suggest you post the related code snippet in your CreateBucketTest.
I believe in your test you are opening a file \var\key. Obviously this is going to cause problem because the file you are thinking of is not really located at \var\key
Some suggestions to you:
instead of reading from absolute path, see if you can change it to opening stream for class path resources.
Put your key file under src/main/test/resources. Assume you put it under src/main/test/resources/cloudkey/key
With the above setup, when you run your test, the key file will be under the classpath at location /cloudkey/key
Edit:
After reading a bit on your code, here are some suggestions:
First, your getStorage relies on system property which is not very test friendly. Consider writing your code in a more test-friendly way
However I believe you are not going to refactor your code anyway, here is one way you can do:
put key in /src/test/resources. By doing so, when compiled, key will be put to BASE_DIR/target/test-classes/key.
Here I assume you will have only 1 key for all your tests. What you need to do is to set the system property private.key.path with the correct path of key. You can do so by configuring surefire plugin : http://maven.apache.org/surefire/maven-surefire-plugin/examples/system-properties.html. Add a systemPropertyVariables private.key.path with value ${project.build.testOutputDirectory}/key. By doing so, when your test is running, the sys property private.key.path will be set with the correct value, so that your test should run fine.

Missing Maven dependencies in Eclipse multi-module project

I’m using STS 2.9.1 (build on Eclipse 3.7.2) with m2e plugin bundled with STS (v1.0.200.20111228-1245).
I have a problem regarding missing dependencies in Eclipse project that contains several modules, or maybe I don’t fully understand how it should work.
It’s a maven project.
In my Project > Properties > Java Build Path > Libraries I have “Maven Dependencies” library, but it's empty (and that’s the problem).
The main POM doesn’t have any dependencies, but it has several modules declared in it.
Adding a dependency to module’s POM doesn’t add it to the “Maven Dependencies” library (what was my expectation) and leads to Eclipse showing errors in source files.
Adding a dependency to the main POM adds it to the “MD” lib, but of course I don’t want to add all of my modules’ dependencies to the main POM just to have it in “MD” lib and adding every single dependency to the Build Path doesn’t seem right nor practical.
I’ve tried:
Project > Clean,
Maven > Update dependencies,
Maven > Update project configuration,
Unchecking the checkbox: Project > Properties > Maven > Resolve dependencies from Workspace projects.
None of the above seems to do the trick.
Example:
Simplified project structure:
simple.project
...
sample-module
...
pom.xml
pom.xml
simple.project/pom.xml:
<project ...>
<modelVersion>4.0.0</modelVersion>
<groupId>test</groupId>
<artifactId>simple.project</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>sample-module</module>
</modules>
<dependencies>
<dependency><!-- This dependency is present in "MD" lib. -->
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
simple.project/sample-module/pom.xml:
<project ...>
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>simple.project</artifactId>
<groupId>test</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>test</groupId>
<artifactId>sample-module</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency><!-- I've expected this dependency also to appear in "MD" lib. -->
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
It is not supposed to work. A project only imports a dependency from another one if it depends on that project (using dependency) or if it inherits from it (using parent). The module element only represents an aggregation.
The question is from time ago, but I solved this creating a Maven Project and adding Maven Modules: right click on project and "New > Project... > Maven > Maven Module".
After that, no more errors were shown in code.
First thing that I see is that you're defining dependencies in a pom parent. There I would expect to see a <dependencyManagement> (see here the doc) structure. In this way the submodules will inherit properly those common dependencies.
Aside from that lets start for the most simple test. Try to compile your project from the maven utility in the command line. If it works then you have a problem in your Eclipse configuration, otherwise the problem is in the way you have defined your project.
If your project compiles properly from the command line, lets see what else can be happening.
The fact that the Maven Dependencies Library is empty means that the Eclipse Maven plugin is not resolving properly your poms. I had quite bad experiences with the embedded STS maven plugin. Try to downgrade it to the m2e 0.10 version. You only need to open the STS DashBoard / Find Updates / Install m2e 0.10
I hope some of these tips can help you.

Porting a tomcat web project from eclipse ganymede to intellij 8.1

I have a standard (I think) web project developed with the eclipse IDE. I wish to port it to Intellij idea 8.1 - I think that, among other things, it has better taglib support.
My project structure is as follows:
Project Folder
./src [java source files etc.]
./conf [configuration files - log4j, spring beans...]
./buid [ant files]
./WebContent
./WebContent/images [image files]
./WebContent/META-INF
./WebContent/META-INF/context.xml
./WebContent/pages [.jsp+.html files]
./WebContent/scripts [.js files]
./WebContent/skins [.css files]
./WebContent/WEB-INF
./WebContent/WEB-INF/classes [.class files]
./WebContent/WEB-INF/lib [.jar files]
./WebContent/WEB-INF/tags [.tag files]
./WebContent/WEB-INF/web.xml
I can't seem to get this project configured with my local tomcat server (version: apache-tomcat-6.0.18).
I think that a good answer would be a standard, step by step, cookbook answer as to how to port (and perhaps how to correctly define a tomcat web application within intellij idea).
Thanks all!
I think the first step would be to create a stand-alone build file which will produce a WAR. Do this before attempting to import the project into InteliJ.
I would use Maven. Creating a maven POM file to create a WAR is almost trivial and you can easily override the the default locations for your src, conf, and web content to match you existing src directory. Then test the build by deploying your newly Maven created WAR to Tomcat. I wouldn't think this first task would take more than a half day (at most a full day).
IntelliJ has a built in utility to import Maven projects. Then you should be off and running....
Regardless of the IDE you finally settle on, your project will be much better off in the long run for the Maven migration.
You initial Maven POM file will look something like 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>com.yourcompany.yourapp</groupId>
<artifactId>yourapp</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>Your project name here</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
*** other dependencies here ***
</dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<resources>
<resource>
<directory>conf</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.0</version>
<configuration>
<webResources>
<resource>
<!-- this is relative to the pom.xml directory -->
<directory>WebContent</directory>
</resource>
</webResources>
</configuration>
</plugin>
</plugins>
</build>
</project>
*** This is an example POM only... It's just meant to get you started and may not work "as is".
Start off by creating an empty web application for tomcat, within IntelliJ - and make sure that it deploys correctly
This will produce a directory structure that you should then be able to copy your source files/web assets into.
The thing that you'll probably need to handle differently is the lib files - don't store these directly in the WEB-INF directory, as keeping them in a separate 'library' area, and allowing the IDE to include them in the WAR at build time is generally a better approach, as it promotes re-use across projects.
The key thing to aim for is to not try to set your project up to completely mirror a tomcat application, as the build process will pull together the various parts for you. It all breaks down into 3 sections...
Static assets - images, config files and jsp files (Ok, I know JSP files are kinda dynamic)
Java classes - source code that you write yourself (The IDE will compile these and place them in the appropriate location)
Java Libraries - third party code that you compile against (Again the IDE will place these in the appropriate location)
There are a few bits of configuration, within the project file, that you'll need to tweak to suit your needs, but it's generally straightforward.
By default, log4j will look for it's configuration file (either log4j.xml or log4j.properties) from the classpath of your application. So this means you should place it in WEB-INF\classes, or you can specify a different location with the environment variable log4j.configuration. See the log4j manual.
What IDE you use should have no impact on the structure of your application when it gets deployed to your servlet container. It sounds like maybe you were relying on Eclipse to package the files in a specific way - this is probably a bad practice. Are you using an actual build script?