I'm trying to set up a multi-module Maven project, and the inter-module dependencies are apparently not being set up correctly.
I have:
<modules>
<module>commons</module>
<module>storage</module>
</modules>
in the parent POM (which has a packaging-type pom)
and then subdirectories commons/ and storage/ which define JAR poms with the same name.
Storage depends on Commons.
In the main (master) directory, I run mvn dependency:tree and see:
[INFO] Building system
[INFO] task-segment: [dependency:tree]
[INFO] ------------------------------------------------------------------------
[INFO] [dependency:tree {execution: default-cli}]
[INFO] domain:system:pom:1.0-SNAPSHOT
[INFO] \- junit:junit:jar:3.8.1:test
[INFO] ------------------------------------------------------------------------
[INFO] Building commons
[INFO] task-segment: [dependency:tree]
[INFO] ------------------------------------------------------------------------
[INFO] [dependency:tree {execution: default-cli}]
...correct tree...
[INFO] ------------------------------------------------------------------------
[INFO] Building storage
[INFO] task-segment: [dependency:tree]
[INFO] ------------------------------------------------------------------------
Downloading: http://my.repo/artifactory/repo/domain/commons/1.0-SNAPSHOT/commons-1.0-SNAPSHOT.jar
[INFO] Unable to find resource 'domain:commons:jar:1.0-SNAPSHOT' in repository my.repo (http://my.repo/artifactory/repo)
[INFO] ------------------------------------------------------------------------
[ERROR] BUILD ERROR
[INFO] ------------------------------------------------------------------------
[INFO] Failed to resolve artifact.
Missing:
----------
1) domain:commons:jar:1.0-SNAPSHOT
Why does the dependency on "commons" fail, even though the reactor has obviously seen it because it successfully processes its dependency tree? It should definitely not be going to the 'net to find it as it's right there...
The pom for storage:
<?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>
<packaging>jar</packaging>
<parent>
<artifactId>system</artifactId>
<groupId>domain</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>domain</groupId>
<artifactId>storage</artifactId>
<name>storage</name>
<url>http://maven.apache.org</url>
<dependencies>
<!-- module dependencies -->
<dependency>
<groupId>domain</groupId>
<artifactId>commons</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!-- other dependencies -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Thanks for any suggestions!
(Edit)
To clarify, what I am looking for here is this: I don't want to have to install module X to build module Y which depends on X, given that both are modules referenced from the same parent POM. This makes intuitive sense to me that if I have two things in the same source tree, I shouldn't have to install intermediate products to continue the build. Hopefully my thinking makes some sense here...
As discussed in this maven mailing list thread, the dependency:tree goal by itself will look things up in the repository rather than the reactor. You can work around this by mvn installing, as previously suggested, or doing something less onerous that invokes the reactor, such as
mvn compile dependency:tree
Works for me.
I think the problem is that when you specify a dependency Maven expects to have it as jar (or whatever) packaged and available from at least a local repo. I'm sure that if you run mvn install on your commons project first everything will work.
Bonusing off the answer from Don Willis:
If your build creates test-jars to share test code among your reactor submodules you should use:
mvn test-compile dependency:tree
which will allow dependency:tree to run to completion in this case.
Realizing this is an older thread but it seems that either the tool evolved or this might have been missed the first time around.
It is possible to perform a build that makes dependencies resolved without installing by doing a reactor build.
If you start your build in the parent that describes the module structure of your project then your dependencies between your modules will be resolved during the build itself through the internal Maven reactor.
Of course this is not the perfect solution since it does not solve the build of a single individual module within the structure. In this case Maven will not have the dependencies in his reactor and will bee looking to resolve it in the repository. So for individual builds you still have to install the dependencies first.
Here is some reference describing this situation.
The only thing that workd for me : switching to gradle :(
I have
Parent
+---dep1
+---war1 (using dep1)
and I can just cd in war1 and use mvn tomcat7:run-war.
I always have to install the whole project before, despite war1 references his parent and the parent references war1 and dep1 (as modules) so all dependencies should be known.
I don't understand what the problem is.
In a Maven module structure like this:
- parent
- child1
- child2
You will have in the parent pom this:
<modules>
<module>child1</module>
<module>child2</module>
</modules>
If you now depend on child1 in child2 by putting the following in your <dependencies> in child2:
<dependency>
<groupId>example</groupId>
<artifactId>child1</artifactId>
</dependency>
You will receive an error that the JAR for child1 cannot be found. This can be solved by declaring a <dependencyManagement> block including child1 in the pom for parent:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>example</groupId>
<artifactId>child1</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
child1 will now be build when you run a compile or package etc. goal on parent, and child2 will find child1's compiled files.
for me, what led me to this thread was a similar problem and the solution was to ensure all module dependency pom's had
<packaging>pom</packaging>
the parent had
pom
my model dep had pom - so there was no jar to be found.
Using version >= 3.1.2 of the dependency plugin seems to solve the issue.
Make sure the module which is failing gets resolved in the pom, is pointing to the right parent by including the configurations in the pom file of the module.
Related
I use IDEA,My springboot app contains multiple modules like that:
parent
-web
-service
-dao
There is a jar dependency common:1.2.0-SNAPSHOT in service module defined in pom.xml, I updated its version to 1.3.0-SNAPSHOT, after that i do maven reimport and run mvn clean install -U but these two versions still in dependencies list, and in dependency tree, two jar in the dependencies, also when i run the app, it throw NoClassDefFoundError error which i added in 1.3.0-SNAPSHOT
This is my maven dependenct tree:
service:jar
+- dao:jar
\- common:jar:1.3.0-SNAPSHOT
web:jar
+- service:jar
+- dao:jar
\- common:jar:1.2.0-SNAPSHOT
\- others:jar
service pom dependency:
<dependency>
<groupId>com</groupId>
<artifactId>common</artifactId>
<version>1.3.0-SNAPSHOT</version>
</dependency>
How can i update the dependency?
As #Haroon #chrylis said, i checked the parent pom.xml and found <parent> configured in this pom.
<parent>
<groupId>insight.plat</groupId>
<artifactId>insight-bom-base</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
in pom insight-bom-base, common 1.3.0-SNAPSHOT is configured, so web module depend this version not version 1.2.0-SNAPSHOT.
<dependencyManagement>
<dependency>
<groupId>com</groupId>
<artifactId>common</artifactId>
<version>1.2.0-SNAPSHOT</version>
</dependencyManagement>
As maven doc Transitive Dependencies shows,
Dependency management - this allows project authors to directly specify the
versions of artifacts to be used when they are encountered in transitive
dependencies or in dependencies where no version has been specified.
In the example in the preceding section a dependency was directly added to A even though
it is not directly used by A.
Instead, A can include D as a dependency in its dependencyManagement section and directly
control which version of D is used when, or if, it is ever referenced.
The parent dependencyManagement has higher priority than Transitive Dependencies, so in service module, it use common 1.2.0-SNAPSHOT
I'm trying to get Maven to download dependencies. I'm using the commands I believe you are supposed to use, and this is just pure command line, no IDE involvement, but it still isn't happening.
The problem is also with my own project, but I'll use this one by someone else as an example: https://github.com/stephanrauh/BeyondJava.net-Articles/tree/master/MethodModificationWithASM
I've tried the following commands:
mvn compile
mvn test
mvn package
mvn install
mvn dependency:resolve
and all appear to run correctly, even going so far as to claim to have successfully downloaded dependencies:
[INFO]
[INFO] The following files have been resolved:
[INFO] javassist:javassist:jar:3.12.1.GA:compile
[INFO] org.ow2.asm:asm:jar:5.0.3:compile
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 16.690 s
[INFO] Finished at: 2020-01-13T16:13:34Z
[INFO] ------------------------------------------------------------------------
but when I try to actually run the compiled program, I get an error:
C:\BeyondJava.net-Articles\MethodModificationWithASM>java -jar target\MethodModificationWithASM-0.0.1-SNAPSHOT.jar
Error: Unable to initialize main class de.beyondjava.demos.bytecode.Main
Caused by: java.lang.NoClassDefFoundError: org/objectweb/asm/ClassVisitor
and the same when I try to run from the .class files instead of the jar. And the error is correct: the asm jar is in fact missing:
C:\BeyondJava.net-Articles\MethodModificationWithASM>dir /s *.jar
Volume in drive C is Windows
Volume Serial Number is 04EE-7EB0
Directory of C:\BeyondJava.net-Articles\MethodModificationWithASM\target
13/01/2020 16:01 8,144 MethodModificationWithASM-0.0.1-SNAPSHOT.jar
1 File(s) 8,144 bytes
Total Files Listed:
1 File(s) 8,144 bytes
0 Dir(s) 164,672,442,368 bytes free
nor has it been squirreled away anywhere else; I searched the entire hard disk.
The pom.xml seems to specify the dependency okay, and Maven seems to be happy that it has done so:
<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>de.beyondjava.demos.bytecode</groupId>
<artifactId>MethodModificationWithASM</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>MethodModificationWithASM</name>
<description>This demo shows how to create and run byte code in a Java program.
</description>
<dependencies>
<dependency>
<groupId>org.ow2.asm</groupId>
<artifactId>asm</artifactId>
<version>5.0.3</version>
</dependency>
<dependency>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.12.1.GA</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
So what am I missing?
You cannot just run a jar on the command line without a proper classpath. Maven downloads the artifacts to .m2/repository in your home directory and uses them for the build, but it does not copy them to target or add them to the produced jar.
Most jars are used as libraries and for them, the behaviour is just fine. If you want to run a jar from the command line, it is better to create a jar with dependencies:
How can I create an executable JAR with dependencies using Maven?
I want to set a release version for my project. I use an aggregate pom for all modules. Each module parent is not the aggregate pom mentioned before.
My question differs from Updating version numbers of modules in a multi-module Maven project question, there the aggregate pom is a parent pom as well.
The aggregate pom.xml is:
<?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.example</groupId>
<artifactId>aggregate</artifactId>
<version>0.0.1</version>
<packaging>pom</packaging>
<modules>
<module>module1</module>
</modules>
</project>
Where the module1 pom.xml is:
<?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.example</groupId>
<artifactId>module-1</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>com.example</groupId>
<artifactId>parent1</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../../parent1/pom.xml</relativePath>
</parent>
</project>
When I execute:
mvn versions:set -DnewVersion=0.0.1
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO]
[INFO] module-1
[INFO] aggregate
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building aggregate 0.0.1
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- versions-maven-plugin:2.3:set (default-cli) # aggregate ---
[INFO] Searching for local aggregator root...
[INFO] Local aggregation root: C:\Users\maxim.kirilov\workspace\maven-games\aggregate
[INFO] Processing change of com.example:aggregate:0.0.1 -> 0.0.1
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO]
[INFO] module-1 ........................................... SKIPPED
[INFO] aggregate .......................................... SUCCESS [ 0.962 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
Why module-1 is skipped? I read some articles that suggests to invoke the maven set version command for each module separately, does maven support for cleaner solution?
Recently (23/09/2017) a documentation was added to Versions Maven Plugin. In order to update all project under aggregate project you need to add supplemental command line parameters:
mvn versions:set -DnewVersion=0.0.1 -DoldVersion=* -DgroupId=* -DartifactId=*
This is needed otherwise the filter for the versions (via -DoldVersion=) would have filtered out that module cause it has a different version than the rest of modules. Furthermore you need to add the -DgroupId= and -DartifactId=* otherwise the module-1 will also filtered out based on the differences in groupId and artifactId.
You may need to include Versions Maven Plugin in your aggregate pom xml.
http://www.mojohaus.org/versions-maven-plugin/
http://www.mojohaus.org/versions-maven-plugin/usage.html
something like this i do:
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>versions-maven-plugin</artifactId>
<version>2.3</version>
<configuration>
<autoVersionSubmodules>true</autoVersionSubmodules>
</configuration>
</plugin>
</plugins>
I am having trouble running a java application (static void main) with the m2e plugin. I have created a multi-module project through the ordinary eclipse wizard. It seems as if code in the test directory of one module cannot reference code in the test directory of another module at runtime (compiling works just fine).
Version information
Eclipse = Luna, Build 4.4.0, Build id 20140612-0600
m2e = 1.5.0.20140606-0033
I have created an extremely simple (and contrived) example to demonstrate my problem. Please go easy on me for the pointlessness of this code. Also, forgive me for the verbosity here. Sadly maven questions almost require it.
Here is the project directory structure:
--Project Explorer
|--animals/
|--src/test/java/
|--com.example.problem.animals
|--Animal.java
|--JRE System Library [JavaSE-1.8]
|--src/
|--target/
|--pom.xml
|--dogs/
|--src/test/java/
|--com.example.problem.animals
|--Beagle.java
|--JRE System Library [JavaSE-1.8]
|--Maven Dependencies
|--animals/
|--src/
|--target/
|--pom.xml
|--parent/
|--animals/
|--dogs/
|--src/
|--pom.xml
The parent module 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>com.example.problem</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>Parent</name>
<description>Parent module for example project.</description>
<modules>
<module>animals</module>
<module>dogs</module>
</modules>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
The animals module 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>
<parent>
<groupId>com.example.problem</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>animals</artifactId>
<name>Animals</name>
<description>Module to hold common animal code.</description>
</project>
The dogs module 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>
<parent>
<groupId>com.example.problem</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>dogs</artifactId>
<name>Dogs</name>
<description>Module to hold dog specific code.</description>
<dependencies>
<dependency>
<groupId>com.example.problem</groupId>
<artifactId>animals</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
The idea here is to have the Beagle object annotated with #Animal. The annotation lives under the test directory of "animals", while our java runnable class Beagle lives under the test directory of "dogs".
Animal.java:
package com.example.problem.animals;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Simple annotation to denote an animal.
*
* #author Rich - created 03/Sep/2014
*/
#Retention(RetentionPolicy.RUNTIME)
#Target(ElementType.TYPE)
public #interface Animal {
String noise();
}
Beagle.java:
package com.example.problem.animals;
/*
* Runnable java class that simply prints the animal noise for Beagle to the console.
*
* #author Rich - created 03/Sep/2014
*/
#Animal(noise = "Woof!")
public class Beagle {
public static void main(String[] args) {
Animal animal = Beagle.class.getAnnotation(Animal.class);
System.out.println(animal.noise());
}
}
In order to run the Beagle class, I have created a new run configuration using the eclipse wizard. The configuration type is "Maven Build". The "JRE" tab has the "Runtime JRE" set to be "Workspace default JRE (jre1.8.0_20)". The following settings are made on the "Main" tab of the configuration:
"Base directory" field is set to "${workspace_loc:/dogs}"
"Goals" field is set to "exec:java"
"Profiles" field is not set
"User settings" field is not set
"Offline", "Update Snapshots", "Debug Output", "Skip Tests", and "Non-recursive" checkboxes are not checked
"Resolve Workspace artifacts" checkbox is checked
Parameters that are added
"exec.mainClass" is set to "com.example.problem.animals.Beagle"
"exec.classpathScope" is set to "test"
"Maven Runtime" is set to "EMBEDDED (3.2.1/1.5.0.20140605-2032)"
Running this configuration ultimately fails and produces the following console output:
[INFO] Scanning for projects...
[INFO]
[INFO] Using the builder org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder with a thread count of 1
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Dogs 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- exec-maven-plugin:1.3.2:java (default-cli) # dogs ---
[WARNING] Warning: killAfter is now deprecated. Do you need it ? Please comment on MEXEC-6.
[WARNING]
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:293)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NoClassDefFoundError: com/example/problem/animals/Animal
at com.example.problem.animals.Beagle.main(Beagle.java:6)
... 6 more
Caused by: java.lang.ClassNotFoundException: com.example.problem.animals.Animal
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 7 more
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.282 s
[INFO] Finished at: 2014-09-13T18:25:51-08:00
[INFO] Final Memory: 9M/155M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.3.2:java (default-cli) on project dogs: An exception occured while executing the Java class. null: InvocationTargetException: com/example/problem/animals/Animal: com.example.problem.animals.Animal -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
After running the configuration with "Debug Output" set, I dug through the console and found the following interesting lines:
[DEBUG] Configuring mojo 'org.codehaus.mojo:exec-maven-plugin:1.3.2:java' with basic configurator -->
[DEBUG] (f) arguments = []
[DEBUG] (f) classpathScope = test
[DEBUG] (f) cleanupDaemonThreads = true
[DEBUG] (f) daemonThreadJoinTimeout = 15000
[DEBUG] (f) includePluginDependencies = false
[DEBUG] (f) includeProjectDependencies = true
[DEBUG] (f) keepAlive = false
[DEBUG] (f) killAfter = 1
[DEBUG] (f) localRepository = id: local
...
[DEBUG] Project Dependencies will be included.
[DEBUG] Collected project artifacts [com.example.problem:animals:jar:0.0.1-SNAPSHOT:compile]
[DEBUG] Collected project classpath [C:\Users\cairnsjr13\workspace\parent\dogs\target\test-classes, C:\Users\cairnsjr13\workspace\parent\dogs\target\classes]
[DEBUG] Adding to classpath : file:/C:/Users/cairnsjr13/workspace/parent/dogs/target/test-classes/
[DEBUG] Adding to classpath : file:/C:/Users/cairnsjr13/workspace/parent/dogs/target/classes/
[DEBUG] Adding project dependency artifact: animals to classpath
The reason I am so confused is the fact that it looks like it is trying to include the "animals" dependency. I suspect its including the main dependency and not the test dependency. So... after this extremely long winded info dump... Does anyone have any idea how to get eclipse (m2e) to execute this situation? I have gotten it configured properly to handle the test-test compile dependency, but cannot for the life of me get the runtime dependency to work.
Setting exec.classpathScope to test adds the test classes of the executed module to the classpath but not the test classes of the dependency. As shown in the debug output, it adds the test classes of the dogs module but does not add the test classes of animals:
[DEBUG] Adding to classpath : file:/C:/Users/cairnsjr13/workspace/parent/dogs/target/test-classes/
In order to depend on the test classes of animals, you would need to configure a maven-jar-plugin for it by specifying the test-jar goal as explained in this link.
animals
<build>
<plugins>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Then update the dependency of dogs on animals to have a test scope as follows:
dogs
<dependencies>
<dependency>
<groupId>com.example.problem</groupId>
<artifactId>animals</artifactId>
<version>0.0.1-SNAPSHOT</version>
<type>test-jar</type>
<scope>test</scope>
</dependency>
</dependencies>
After adding this configuration, install the animals module into your local Maven repository. This would create a Jar in your local repo that contains the test classes of the module, specifically the Animal class, along with the normal Jar that's created for the main classes. You can either do the install from the command line or right-clicking on the animals project, and selecting Run As -> Maven install.
Then launch the same run configuration in Eclipse but with the "Resolve Workspace artifacts" checkbox unchecked so that the test artifacts are resolved from the local Maven repository.
The weird thing I noticed, when reproducing your scenario, is that if I add all of the above Maven configuration (the maven-jar-plugin and the test-scoped dependency) but I keep the "Resolve Workspace artifacts" checkbox ticked, m2eclipse fails to resolve the test Jar in the workspace even if the target folder contains the Jar and the project is refreshed/updated. I have a feeling that it's a bug in the plugin itself, specifically when resolving a workspace test Jar dependency configured using the test-jar goal.
After a ton of incremental changes and trial-and-error, I've figured out how to get this to work. The nice thing about this approach is that you can still actively develop in the animals project without having to export it as a test-jar everytime. The <dependency> tag has a type attribute which must be set. The dogs pom.xml file then becomes:
<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>
<parent>
<groupId>com.example.problem</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>dogs</artifactId>
<name>Dogs</name>
<description>Module to hold dog specific code.</description>
<dependencies>
<dependency>
<groupId>com.example.problem</groupId>
<artifactId>animals</artifactId>
<version>0.0.1-SNAPSHOT</version>
<type>test-jar</type>
</dependency>
</dependencies>
</project>
Executing the original run configuration yields the following output:
[INFO] Scanning for projects...
[INFO]
[INFO] Using the builder org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder with a thread count of 1
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Dogs 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- exec-maven-plugin:1.3.2:java (default-cli) # dogs ---
[WARNING] Warning: killAfter is now deprecated. Do you need it ? Please comment on MEXEC-6.
Woof!
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.310 s
[INFO] Finished at: 2014-09-21T13:53:21-08:00
[INFO] Final Memory: 9M/155M
[INFO] ------------------------------------------------------------------------
There are a couple of things to note here.
The scope attribute is not required to be set to test to get this to run. Adding the <scope>test</scope> tag to the dependency will cause the dependency to be omitted in a non-test scope. Since we are running in test scope it does not matter.
This approach will NOT include a dependency on the main code from animals. In the example in the original question, this did not matter because there was no main code source directory. In the event that the test code in animals depends on main code in animals you must add an additional dependency in dogs to pick that up. Here is what the dogs pom.xml file would look like in that situation:
<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>
<parent>
<groupId>com.example.problem</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>dogs</artifactId>
<name>Dogs</name>
<description>Module to hold dog specific code.</description>
<dependencies>
<!-- Including both dependencies for jar and test-jar to get main src included. -->
<dependency>
<groupId>com.example.problem</groupId>
<artifactId>animals</artifactId>
<version>0.0.1-SNAPSHOT</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>com.example.problem</groupId>
<artifactId>animals</artifactId>
<version>0.0.1-SNAPSHOT</version>
<type>test-jar</type>
</dependency>
</dependencies>
</project>
I'm trying to start using flyway with maven integration but can't make it work.
I'm following the documentation seems to be very simple so no strange things seem to be done.
My pom.xml is the following:
<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.test</groupId>
<artifactId>test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<plugins>
<!-- Flyway plugin configuration -->
<plugin>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-maven-plugin</artifactId>
<version>3.0</version>
<configuration>
<url>jdbc:mysql://localhost:3306/test</url>
<user>test_fede</user>
<password>test_fede</password>
</configuration>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.21</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>13.0.1</version>
</dependency>
<!-- DB dependencies -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.21</version>
</dependency>
<!-- Test dependencies -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.1</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
I have the directory resources/db/migration/ without any migration yet.
When I issuing flyway:info on cygwin or cmd I got an flyway error:
$ mvn compile flyway:info
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building Unnamed - com.test:test:jar:0.0.1-SNAPSHOT
[INFO] task-segment: [compile, flyway:info]
[INFO] ------------------------------------------------------------------------
[INFO] [resources:resources {execution: default-resources}]
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO] [compiler:compile {execution: default-compile}]
[INFO] Nothing to compile - all classes are up to date
[INFO] [flyway:info {execution: default-cli}]
[INFO] Database: jdbc:mysql://localhost:3306/test (MySQL 5.5)
[INFO] ------------------------------------------------------------------------
[ERROR] BUILD ERROR
[INFO] ------------------------------------------------------------------------
[INFO] org.flywaydb.core.api.FlywayException: Unable to scan for SQL migrations in location: classpath:db/migration
Embedded error: Unable to determine URL for classpath location: db/migration (ClassLoader: org.codehaus.classworlds.RealmClassLoader#5bcdbf6)
[INFO] ------------------------------------------------------------------------
[INFO] For more information, run Maven with the -e switch
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1 second
[INFO] Finished at: Tue May 06 11:06:15 CST 2014
[INFO] Final Memory: 17M/223M
[INFO] ------------------------------------------------------------------------
Can give me a hand on this?
Thans a lot.
This also happens if the compile goal is not executed before calling flyway:migrate. Actually this IS included in the quick start manual. It says:
mvn compile flyway:migrate
However if you miss that detail and start to just call mvn flyway:migrate, the SQL file will not get copied into the target directory (actually the target directory will not even exist) and you get this cryptic error.
Well, just for you to know.
I found the problem, it happens when we setup flyway in our environment but we don't have any migration to be executed.
It shouldn't display a classpath error but fortunately it's working.
By the way, another problem I found is that after executing init if we check with info nothing is displayed. And if we add a new migration with V1 then info won't show it unless we change it to V1_1
Hope to help
I had a similar problem and it was because when I created the migration directory I gave it the name db.migration directly.
By creating the directory db and then inside it the migration directory it worked.
I ran into the same problem. In my case, I had my migration script in the wrong directory that had caused the issue. I moved the script V1__Create_person_table.sql to the right directory at resources/db/migration/ and it worked!!
I faced the same issue. But when i obeserved the logs keenly i found flywaydb is looking in db/migration folder for the script but my script is in db/migrate. so, after correcting the path from db/migrate to db/migration it works!!.
Here's a dumb mistake I've made where I got this pesky error:
Make sure you set your packaging as jar not pom in your pom.xml. Then mvn install and make sure you have a jar for that project in your M2 folder otherwise you will get this error since the migration file wasn't copied over and found.
I had a similar problem too. in my case i thought i had named the directories correctly after triple checking everything. Turned out that i typed migrations instead of migration. As soon as i fixed this everything was fine.
Just before this i had another issue with the underscore after the version marker (V1) in the migration filename. It needs to be a dunder (double underscore) so its always V1__description.sql .
Seems like none of the flyway issues i encounter are big mess-up's. it's always something small that can be easily fixed. It's just about finding out what the problem is. That's the hard part.