With maven1 I was using the extend tag to tell my children project to use their parent configuration.
All dependencies declared in the parent were available in extending (children) projects.
Now with maven2 I'm using the inheritance/composition feature and I have to redeclare my dependencies (minus the version number) in every child project.
(see how-to-share-common-properties-among-several-maven-projects)
Is there a way to tell maven that I want to share some of my dependencies among all my children ?
Now with maven2 I'm using the inheritance/composition feature and I have to redeclare my dependencies (minus the version number) in every child project
No, you don't. Dependencies declared in the parent pom are inherited.
Is there a way to tell maven that I want to share some of my dependencies among all my children ?
Just declare the <parent> element in child POMs. For example, with this parent 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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>my.group.id</groupId>
<artifactId>parent</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<name>Demo - Parent</name>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<modules>
<module>child</module>
</modules>
</project>
And this POM for the child module:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>my.group.id</groupId>
<artifactId>parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<name>Demo - Child</name>
<artifactId>child</artifactId>
<packaging>jar</packaging>
</project>
The junit dependency gets inherited as expected:
$ mvn dependency:tree
[INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: 'dependency'.
[INFO] ------------------------------------------------------------------------
[INFO] Building Demo - Child
[INFO] task-segment: [dependency:tree]
[INFO] ------------------------------------------------------------------------
[INFO] [dependency:tree {execution: default-cli}]
[INFO] my.group.id:child:jar:1.0-SNAPSHOT
[INFO] \- junit:junit:jar:3.8.1:test
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
...
I suspect that you are declaring dependencies in the <dependencyManagement> section (which has another purpose).
Related
I have a Maven project that generates a WAR file and tries to get the artifact version from properties in .properties files that are maintained in the codebase. I also try to form the final name of the WAR file using custom properties.
Snippet :
<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.xyz.webapps</groupId>
<artifactId>webapps</artifactId>
<version>${info.version}-${application.env}</version>
<packaging>war</packaging>
<!-- Filling in the artifact version with properties read below -->
...
<!-- Filling in the WAR name -->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.war.final.name>${pom.artifactId}-${pom.currentVersion}.war</maven.war.final.name>
<maven.test.skip>true</maven.test.skip>
</properties>
...
<build>
<plugins>
...
<!-- I read those properties files here -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0-alpha-2</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>read-project-properties</goal>
</goals>
<configuration>
<files>
<file>${project.basedir}/webapps-main/src/main/resources/MessageResources.properties</file>
<file>${project.basedir}/build.properties</file>
</files>
</configuration>
</execution>
</executions>
</plugin>
...
</plugins>
...
</project>
The name of the WAR file gets generated properly when I do a "mvn clean package" :
[INFO] Assembling webapp[webapps] in [/home/jubuntu/workspace/ui/new/webapps/target/webapps-2.8-qa]
[INFO] Processing war project
[INFO] Copying webapp resources[/home/jubuntu/workspace/ui/new/webapps/webapps-main/src/main/webapp]
[INFO] Webapp assembled in [4690 msecs]
[INFO] Building war: /home/jubuntu/workspace/ui/new/webapps/target/webapps-2.8-qa.war
But when I do a "mvn clean install" ( or a "mvn clean deploy" ) , the properties don't expand for some reason (the package phase still generates the WAR with the right name) :
[INFO] Building war: /home/jubuntu/workspace/ui/new/webapps/target/webapps-2.8-qa.war
[INFO] [install:install {execution: default-install}]
[INFO] Installing /home/jubuntu/workspace/ui/new/webapps/target/webapps-2.8-qa.war to /home/jubuntu/.m2/repository/com/xyz/webapps/webapps/${info.version}-${application.env}/webapps-${info.version}-${application.env}.war
Is there something wrong that I'm doing here ? How would I make this work for installing and deploying my artifact ? I use maven version 2.2.1 for my builds. Thanks.
You can't do this. It's not supported by Maven. It's fundamental.
I tried to build a project. It failed. I made the correct changes and tried to build it again. I relieve the following error:
Embedded error: Directory simple already exists - please run from a clean directory
dan#dan-netbook:~/Documents/Maven/mavenbook-examples-1-SNAPSHOT/ch03-simple$ mvn archetype:create -DgroupId=org.sonatype.mavenbook.ch03 -DartifactId=simple -DpackageName=org.sonatype.mavenbook -e
+ Error stacktraces are turned on.
[INFO] Scanning for projects...
[INFO] Reactor build order:
[INFO] Chapter 3 Parent Project
[INFO] Chapter 3 Simple Project Example
[INFO] Searching repository for plugin with prefix: 'archetype'.
[INFO] ------------------------------------------------------------------------
[INFO] Building Chapter 3 Parent Project
[INFO] task-segment: [archetype:create] (aggregator-style)
[INFO] ------------------------------------------------------------------------
[INFO] [archetype:create {execution: default-cli}]
[WARNING] This goal is deprecated. Please use mvn archetype:generate instead
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Old (1.x) Archetype: maven-archetype-quickstart:RELEASE
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: groupId, Value: org.sonatype.mavenbook.ch03
[INFO] Parameter: packageName, Value: org.sonatype.mavenbook
[INFO] Parameter: package, Value: org.sonatype.mavenbook
[INFO] Parameter: artifactId, Value: simple
[INFO] Parameter: basedir, Value: /home/dan/Documents/Maven/mavenbook-examples-1-SNAPSHOT/ch03-simple
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[ERROR] BUILD ERROR
[INFO] ------------------------------------------------------------------------
[INFO] Error creating from archetype
Embedded error: Directory simple already exists - please run from a clean directory
[INFO] ------------------------------------------------------------------------
[INFO] Trace
org.apache.maven.lifecycle.LifecycleExecutionException: Error creating from archetype
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoals(DefaultLifecycleExecutor.java:719)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeStandaloneGoal(DefaultLifecycleExecutor.java:569)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoal(DefaultLifecycleExecutor.java:539)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoalAndHandleFailures(DefaultLifecycleExecutor.java:387)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeTaskSegments(DefaultLifecycleExecutor.java:284)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.execute(DefaultLifecycleExecutor.java:180)
at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:328)
at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:138)
at org.apache.maven.cli.MavenCli.main(MavenCli.java:362)
at org.apache.maven.cli.compat.CompatibleMain.main(CompatibleMain.java:60)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:616)
at org.codehaus.classworlds.Launcher.launchEnhanced(Launcher.java:315)
at org.codehaus.classworlds.Launcher.launch(Launcher.java:255)
at org.codehaus.classworlds.Launcher.mainWithExitCode(Launcher.java:430)
at org.codehaus.classworlds.Launcher.main(Launcher.java:375)
Caused by: org.apache.maven.plugin.MojoExecutionException: Error creating from archetype
at org.apache.maven.archetype.mojos.MavenArchetypeMojo.execute(MavenArchetypeMojo.java:243)
at org.apache.maven.plugin.DefaultPluginManager.executeMojo(DefaultPluginManager.java:490)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoals(DefaultLifecycleExecutor.java:694)
... 17 more
Caused by: org.apache.maven.archetype.old.ArchetypeTemplateProcessingException: Directory simple already exists - please run from a clean directory
at org.apache.maven.archetype.old.DefaultOldArchetype.createArchetype(DefaultOldArchetype.java:251)
at org.apache.maven.archetype.old.DefaultOldArchetype.createArchetype(DefaultOldArchetype.java:117)
at org.apache.maven.archetype.mojos.MavenArchetypeMojo.execute(MavenArchetypeMojo.java:227)
... 19 more
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 7 seconds
[INFO] Finished at: Sun Dec 12 18:53:08 EST 2010
[INFO] Final Memory: 10M/26M
[INFO] ------------------------------------------------------------------------
This leads me to believe that I need to clean the directory or something. I call mvn clean but this does not fix the problem. Im sure this is a very basic question, but Im just learning. Any suggestions?
Parent:
<pre>
<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>
<artifactId>examples</artifactId>
<groupId>org.sonatype.mavenbook</groupId>
<version>1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>Maven: The Definitive Guide Example Code</name>
<description>Example Code for Maven: The Definitive Guide</description>
<url>http://sonatype.com/book</url>
<modules>
<module>ch03-simple</module>
<module>ch04-custom</module>
<module>ch05-simple-web</module>
<module>ch06-multi</module>
<module>ch07-multi-spring</module>
<module>ch08-optimize</module>
<module>ch09-pom</module>
<module>ch10-lifecycle</module>
<module>ch11-profile</module>
<module>ch12-assembly</module>
<module>ch13-properties</module>
<module>ch15-sitegen</module>
<module>ch17-writing-plugins</module>
<module>ch18-alternate-plugins</module>
</modules>
<build>
<finalName>mavenbook-examples-${project.version}</finalName>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>project</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>examples</id>
<phase>package</phase>
<goals>
<goal>attached</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>publish</id>
<distributionManagement>
<site>
<id>sonatype-site</id>
<name>Sonatype Book Examples</name>
<url>file:///var/www/domains/sonatype.com/www/htdocs/book/book-examples/</url>
</site>
</distributionManagement>
<properties>
<sonatype.site>file:///var/www/domains/sonatype.com/www/htdocs/</sonatype.site>
</properties>
</profile>
<profile>
<id>stage</id>
<distributionManagement>
<site>
<id>sonatype-site</id>
<name>Sonatype Book Examples</name>
<url>file:///var/www/domains/sonatype.com/www/htdocs/book-stage/book-examples/</url>
</site>
</distributionManagement>
</profile>
</profiles>
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>2.0.1</version>
</plugin>
</plugins>
</reporting>
<repositories>
<repository>
<id>sonatype-forge</id>
<name>Sonatype Forge</name>
<url>http://repository.sonatype.org/content/groups/public</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>Codehaus Snapshots</id>
<url>http://snapshots.repository.codehaus.org</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
<releases>
<enabled>false</enabled>
</releases>
</repository>
<repository>
<id>Central</id>
<url>http://repo1.maven.org/maven2</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
<releases>
<enabled>true</enabled>
</releases>
</repository>
</repositories>
</project>
</pre>
Other Pom
<pre>
<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>
<parent>
<groupId>org.sonatype.mavenbook</groupId>
<artifactId>examples</artifactId>
<version>1-SNAPSHOT</version>
</parent>
<groupId>org.sonatype.mavenbook.ch03</groupId>
<artifactId>parent</artifactId>
<packaging>pom</packaging>
<name>Chapter 3 Parent Project</name>
<url>http://sonatype.com/book</url>
<modules>
<module>simple</module>
</modules>
</project>
</pre>
Thanks
just in case anyone else hits this problem...I ran into the same issue (just now learning maven) and here's what worked for me:
if you downloaded the code, don't run the command from the sample chapter directory, e.g. /home/uruser/maven/mavenbook-examples-1-SNAPSHOT/ch03-simple
Just setup a new dir like /home/uruser/maven/myTestChapterThree and then run the following:
mvn archetype:create -DgroupId=org.sonatype.mavenbook.ch03 -DartifactId=simple -DpackageName=org.sonatype.mavenbook -e
rookie stuff to be sure, but at least three maven noobs (myself included) have hit this wall so it could be the book's advice to download the sample chapters and then run this command that might be the issue :)
In your Application, I am confused with ArtifactId since, I see you have give as "example" where as you are passing your argument as "simple". Here is the simple example for creating a new artifact.
1. Create a new project and pom.xml for the archetype artifact
An example pom.xml for an archetype artifact looks as follows:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>my.groupId</groupId>
<artifactId>my-archetype-id</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
</project>
All you need to specify is a groupId, artifactId and version. These three parameters will be needed later for invoking the archetype via archetype:create from the commandline.
2. Create the archetype descriptor
The archetype descriptor is a file called archetype.xml which must be located in the src/main/resources/META-INF/maven/ directory. An example of an archetype descriptor can be found in the quickstart archetype:
<archetype
xmlns="http://maven.apache.org/plugins/maven-archetype-plugin/archetype/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org
/plugins/maven-archetype-plugin/archetype/1.0.0
http://maven.apache.org/xsd/archetype-1.0.0.xsd">
<id>quickstart</id>
<sources>
<source>src/main/java/App.java</source>
</sources>
<testSources>
<source>src/test/java/AppTest.java</source>
</testSources>
</archetype>
The tag should be the same as the artifactId in the archetype pom.xml.
An optional true tag makes it possible to run the archetype:create even on existing projects.
The , , , and tags represent the different sections of the project:
<sources> = src/main/java
<resources> = src/main/resources
<testSources> = src/test/java
<testResources> = src/test/resources
<siteResources> = src/site
<sources> and <testSources> can contain <source>
elements that specify a source file.
and can contain elements that specify a resource file.
Place other resources such as the ones in the src/main/webapp directory inside the tag.
At this point one can only specify individual files to be created but not empty directories.
Thus the quickstart archetype shown above defines the following directory structure:
archetype
|-- pom.xml
`-- src
`-- main
`-- resources
|-- META-INF
| `-- maven
| `--archetype.xml
`-- archetype-resources
|-- pom.xml
`-- src
|-- main
| `-- java
| `-- App.java
`-- test
`-- java
`-- AppTest.java
3. Create the prototype files and the prototype pom.xml
The next component of the archetype to be created is the prototype pom.xml. Any pom.xml will do, just don't forget to the set artifactId and groupId as variables ( ${artifactId} / ${groupId} ). Both variables will be initialized from the commandline when calling archetype:create.
An example for a prototype pom.xml is:
<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>${groupId}</groupId>
<artifactId>${artifactId}</artifactId>
<version>${version}</version>
<packaging>jar</packaging>
<name>A custom project</name>
<url>http://www.myorganization.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
4. Install the archetype and run the archetype plugin
Now you are ready to install the archetype:
mvn install
Now that you have created an archetype, you can try it on your local system by using the following command. In this command, you need to specify the full information about the archetype you want to use (its groupId, its artifactId, its version) and the information about the new project you want to create (artifactId and groupId). Don't forget to include the version of your archetype (if you don't include the version, you archetype creation may fail with a message that version:RELEASE was not found)
mvn archetype:create \
-DarchetypeGroupId=<archetype-groupId> \
-DarchetypeArtifactId=<archetype-artifactId> \
-DarchetypeVersion=<archetype-version> \
-DgroupId=<my.groupid> \
-DartifactId=<my-artifactId>
Once you are happy with the state of your archetype, you can deploy (or submit it to ibiblio) it as any other artifact and the archetype will then be available to any user of Maven.
Alternative way to start creating your Archetype
Instead of manually creating the directory structure needed for an archetype, simply use
mvn archetype:create
-DgroupId=[your project's group id]
-DartifactId=[your project's artifact id]
-DarchetypeArtifactId=maven-archetype-archetype
Afterwhich, you can now customize the contents of the archetype-resources directory, and archetype.xml, then, proceed to Step#4 (Install the archetype and run the archetype plugin).
I make use of dependency POMs which I will then go and include into another projects as as dependency. The problem I am having is while it aggregates the POM with those dependencies, it appears when I declare dependencies of scope, provided, those aren't included.
Is it possible to include provided dependencies in dependency POMs with a scope of provided? I often declare what APIs I need and then include the implementation as a runtime dependency.
If a dependency is provided why can't that dependency be inherited with the same scope so I don't have to declare it?
It is inherited with the same scope. Given the following parent pom.xml:
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.stackoverflow.Q3597684</groupId>
<artifactId>root</artifactId>
<version>1.0-SNAPSHOT</version>
<name>Q3597684 - Root</name>
<packaging>pom</packaging>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
And the following pom.xml that inherits from the root artifact:
<project>
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>root</artifactId>
<groupId>com.stackoverflow.Q3597684</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>child</artifactId>
<packaging>war</packaging>
<name>Q3597684 - Child</name>
<dependencies/>
</project>
Running mvn dependency:tree from the child gives the following output:
$ mvn dependency:tree[INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: 'dependency'.
[INFO] ------------------------------------------------------------------------
[INFO] Building Q3597684 - Child
[INFO] task-segment: [dependency:tree]
[INFO] ------------------------------------------------------------------------
[INFO] [dependency:tree {execution: default-cli}]
[INFO] com.stackoverflow.Q3597684:child:war:1.0-SNAPSHOT
[INFO] +- javax.servlet:servlet-api:jar:2.5:provided
[INFO] \- junit:junit:jar:3.8.1:test
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
The provided servlet-api is there, as expected.
Are you maybe (mis)using the dependencyManagement section?
I googled this and it seems that no one has an answer, yet it seems like such an elementary thing that it should be possible.
I have the following project structure:
parent
---sub-project1
---sub-project2
sub-project2 needs to have sub-project1 as a dependency.
So I have this in sub-project2's pom:
<dependencies>
<dependency>
<artifactId>sub-project1</artifactId>
<groupId>mygroup</groupId>
<version>1.0-SNAPSHOT</version>
</dependency>
....
When I do this, Maven tries to dowload the sub-project1.jar file, which does not exist because it's not ready for the repo yet.
I tried to put a <scope>import</scope> in the dependency, but that didn't work either -- same result.
So what do I have to do to get Maven to look at sub-project1 when building sub-project2?
EDIT Here are some pom snippets:
Parent:
<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>
<prerequisites>
<maven>2.0.9</maven>
</prerequisites>
<modules>
<module>sub-project1</module>
<module>sub-project2</module>
</modules>
....
sub-project1:
<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>parent</artifactId>
<groupId>mygroup</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>sub-project1</artifactId>
....
sub-project2:
<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>parent</artifactId>
<groupId>mygroup</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>sub-project1</artifactId>
<dependencies>
....
<dependency>
<artifactId>sub-project2</artifactId>
<groupId>mygroup</groupId>
<version>1.0-SNAPSHOT</version>
<scope>import</scope>
</dependency>
</dependencies>
The error I'm getting when I got mvn clean install on the parent is:
[ERROR] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Compilation failure
With a lot of classes/package not found errors
You should have a master pom at parent's level, in which you will list the modules of your project.
<modules>
<module>sub-project1</module>
<module>sub-project2</module>>
</modules>
In each subproject you have to reference your parent:
<parent>
<artifactId>parent</artifactId>
<groupId>mygroup</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
And you specify the dependencies between the project just as you did. I think you've missed some of the steps I've described.
Edit: you should issue your mvn clean install at the parent level.
When I do this, Maven tries to dowload the sub-project1.jar file, which does not exist because it's not ready for the repo yet.
That's the normal behavior, Maven resolves dependencies through the local repository so you need to install sub-project1 first. Actually, the common way to deal with this kind of situation is to launch a reactor build (a multi-modules build) from the parent.
Assuming you are aggregating modules in the parent i.e. you have something like this declared in the "parent" pom.xml:
<modules>
<module>sub-project1</module>
<module>sub-project2</module>>
</modules>
Just cd into the parent directory and launch a reactor build:
$ cd parent
$ mvn install
Maven will then calculate the build order (deducted from the oriented graph made of modules and their dependencies) and run install on all modules in the calculated order (parent first, then sub-project1 and finally sub-project2 for your particular example).
But don't use a scope of type import, you are misusing it here. Remove it.
Update: The question has been updated while I was answering and the POMs shown do no illustrate the situation given in the original question (reversed dependency, probable mistake in the artifact id). But the suggested approach still applies. Remove the <scope>import</scope> on the dependency and start a reactor build from the parent.
I have a project with multiple modules, including on that is responsible for building the final assembly from the artifacts of the other modules. As part of the assembly, I want to include the JavaDocs for two of the other modules. I have updated the pom files for those modules to generate the JavaDoc JAR files, and modified the assembly project to list those JavaDoc Jar files as dependencies. However, when I build the project from the top level, the assembly project tells me that it cannot find the javaDoc jars. If I install all the other modules first, then build the assembly module directly, the assembly will build fine.
How can I get the assembly to build correctly, with all the specified dependencies, when run from the top level project?
Edited to add more info at the request of the responders:
Here's a simplified project I threw together to demonstrate the issue. The directory layout is as follows:
sample/
\--pom.xml
\--module1/
\--pom.xml
\--src/
\--{the usual main/java layout with a single java file, with javadocs}
\--package/
\--pom.xml
\--assemblies/
\--bin.xml
The top level pom.xml, under sample, looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>test</groupId>
<artifactId>project</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>module1</module>
<module>package</module>
</modules>
<build>
<defaultGoal>package</defaultGoal>
</build>
</project>
The module1 pom.xml is a basic project file with an entry for the javadoc plugin:
<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>test</groupId>
<artifactId>module1</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>javadoc-jar</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
The package module pom file specifies dependencies on the module1 Jar file and the module1 JavaDoc jar file:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<!-- The Basics -->
<groupId>test</groupId>
<artifactId>packaging</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<!-- Shared Dependencies -->
<dependencies>
<dependency>
<groupId>test</groupId>
<artifactId>module1</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>test</groupId>
<artifactId>module1</artifactId>
<version>1.0-SNAPSHOT</version>
<classifier>javadoc</classifier>
</dependency>
</dependencies>
<!-- Build Settings -->
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2-beta-4</version>
<configuration>
<descriptors>
<descriptor>assemblies/bin.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>make-assembly</id> <!-- this is used for inheritance merges -->
<phase>package</phase> <!-- append to the packaging phase. -->
<goals>
<goal>single</goal> <!-- goals == mojos -->
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
And finally, the assembly file includes the two dependencies, with the JavaDoc jar file being stored unpacked into the assembled file. I have each dependencySet use strict Filtering to highlight the inability of the assembly plugin to find the specified files.
<assembly>
<id>bin</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<excludes>
<!-- Exclude the Jars that are included in later sections -->
<exclude>test:module1:jar:javadoc</exclude>
</excludes>
<outputDirectory>lib</outputDirectory>
<unpack>false</unpack>
<useTransitiveDependencies>true</useTransitiveDependencies>
<useTransitiveFiltering>false</useTransitiveFiltering>
<useProjectArtifact>false</useProjectArtifact>
</dependencySet>
<dependencySet>
<includes>
<include>test:module1:jar:javadoc</include>
</includes>
<outputDirectory>docs</outputDirectory>
<unpack>true</unpack>
<useTransitiveDependencies>true</useTransitiveDependencies>
<useProjectArtifact>false</useProjectArtifact>
<useStrictFiltering>true</useStrictFiltering>
</dependencySet>
</dependencySets>
</assembly>
Running this project from the top results in the following output:
[INFO] Scanning for projects...
[INFO] Reactor build order:
[INFO] Unnamed - test:module1:jar:1.0-SNAPSHOT
[INFO] Unnamed - test:packaging:pom:1.0-SNAPSHOT
[INFO] Unnamed - test:project:pom:1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] Building Unnamed - test:module1:jar:1.0-SNAPSHOT
[INFO] task-segment: [clean, package]
[INFO] ------------------------------------------------------------------------
[INFO] [clean:clean]
[INFO] Deleting directory /Users/john/Documents/src/workspace/sample/module1/target
[INFO] [resources:resources]
[WARNING] Using platform encoding (MacRoman actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO] [compiler:compile]
[INFO] Compiling 1 source file to /Users/john/Documents/src/workspace/sample/module1/target/classes
[INFO] [resources:testResources]
[WARNING] Using platform encoding (MacRoman actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /Users/john/Documents/src/workspace/sample/module1/src/test/resources
[INFO] [compiler:testCompile]
[INFO] No sources to compile
[INFO] [surefire:test]
[INFO] No tests to run.
[INFO] [jar:jar]
[INFO] Building jar: /Users/john/Documents/src/workspace/sample/module1/target/module1-1.0-SNAPSHOT.jar
[INFO] [javadoc:jar {execution: javadoc-jar}]
[WARNING] Source files encoding has not been set, using platform encoding MacRoman, i.e. build is platform dependent!
Loading source files for package test...
Constructing Javadoc information...
Standard Doclet version 1.5.0_20
Building tree for all the packages and classes...
Generating /Users/john/Documents/src/workspace/sample/module1/target/apidocs/test//AClass.html...
Generating /Users/john/Documents/src/workspace/sample/module1/target/apidocs/test//package-frame.html...
Generating /Users/john/Documents/src/workspace/sample/module1/target/apidocs/test//package-summary.html...
Generating /Users/john/Documents/src/workspace/sample/module1/target/apidocs/test//package-tree.html...
Generating /Users/john/Documents/src/workspace/sample/module1/target/apidocs/constant-values.html...
Generating /Users/john/Documents/src/workspace/sample/module1/target/apidocs/test/class-use//AClass.html...
Generating /Users/john/Documents/src/workspace/sample/module1/target/apidocs/test//package-use.html...
Building index for all the packages and classes...
Generating /Users/john/Documents/src/workspace/sample/module1/target/apidocs/overview-tree.html...
Generating /Users/john/Documents/src/workspace/sample/module1/target/apidocs/index-all.html...
Generating /Users/john/Documents/src/workspace/sample/module1/target/apidocs/deprecated-list.html...
Building index for all classes...
Generating /Users/john/Documents/src/workspace/sample/module1/target/apidocs/allclasses-frame.html...
Generating /Users/john/Documents/src/workspace/sample/module1/target/apidocs/allclasses-noframe.html...
Generating /Users/john/Documents/src/workspace/sample/module1/target/apidocs/index.html...
Generating /Users/john/Documents/src/workspace/sample/module1/target/apidocs/help-doc.html...
Generating /Users/john/Documents/src/workspace/sample/module1/target/apidocs/stylesheet.css...
[INFO] Building jar: /Users/john/Documents/src/workspace/sample/module1/target/module1-1.0-SNAPSHOT-javadoc.jar
[INFO] ------------------------------------------------------------------------
[INFO] Building Unnamed - test:packaging:pom:1.0-SNAPSHOT
[INFO] task-segment: [clean, package]
[INFO] ------------------------------------------------------------------------
[INFO] [clean:clean]
[INFO] Deleting directory /Users/john/Documents/src/workspace/sample/package/target
[INFO] [site:attach-descriptor]
[INFO] [assembly:single {execution: make-assembly}]
[INFO] Reading assembly descriptor: assemblies/bin.xml
[WARNING] The following patterns were never triggered in this artifact exclusion filter:
o 'test:module1:jar:javadoc'
[WARNING] The following patterns were never triggered in this artifact inclusion filter:
o 'test:module1:jar:javadoc'
[INFO] ------------------------------------------------------------------------
[ERROR] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] : org.apache.maven.plugin.assembly.model.Assembly#139c27
Assembly is incorrectly configured: bin
Assembly: bin is not configured correctly: One or more filters had unmatched criteria. Check debug log for more information.
[INFO] ------------------------------------------------------------------------
[INFO] For more information, run Maven with the -e switch
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 12 seconds
[INFO] Finished at: Wed Oct 07 15:23:26 PDT 2009
[INFO] Final Memory: 26M/52M
[INFO] ------------------------------------------------------------------------
I have posted this project for download.
Figured out a solution that seems like it works (at least on the sample app I posted). I modified the inclusion/exclusion entries in the assembly file to wildcard just the type, and the assembly now behaves exactly as expected. The JavaDoc JAR file is not placed in the lib directory, and the JavaDocs are unpacked as intended.
The final assembly file is as follows:
<assembly>
<id>bin</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<excludes>
<!-- Exclude the Jars that are included in later sections -->
<exclude>test:module1:*:javadoc</exclude>
</excludes>
<outputDirectory>lib</outputDirectory>
<unpack>false</unpack>
<useTransitiveDependencies>true</useTransitiveDependencies>
<useTransitiveFiltering>false</useTransitiveFiltering>
<useProjectArtifact>false</useProjectArtifact>
</dependencySet>
<dependencySet>
<includes>
<include>test:module1:*:javadoc</include>
</includes>
<outputDirectory>docs</outputDirectory>
<unpack>true</unpack>
<useTransitiveDependencies>true</useTransitiveDependencies>
<useProjectArtifact>false</useProjectArtifact>
<useStrictFiltering>true</useStrictFiltering>
</dependencySet>
</dependencySets>
</assembly>
Update:
Some quick testing has revealed that the type of JavaDoc Jar files, at least when referenced from a full build, is 'javadoc'. However, when the package module is run stand-alone, that type is not recognized and cannot be retrieved from the local repository. So, it seems that in order to get both build modes (as part of the overall build, and when built independently), you have to wildcard the type for the JavaDoc Jar files in the assembly.
There is a problem with the test:module1:jar:javadoc identity pattern used for exclusion and inclusion of dependencies in both <dependencySet> as shown by in the build failure trace:
[WARNING] The following patterns were never triggered in this artifact exclusion filter:
o 'test:module1:jar:javadoc'
[WARNING] The following patterns were never triggered in this artifact inclusion filter:
o 'test:module1:jar:javadoc'
To be honnest, I can't see what's wrong with the test:module1:jar:javadoc pattern: it follows the groupId:artifactId:type[:classifier] format and looks absolutely fine to me (could this be a bug?). But the fact is that it doesn't match any dependency and this causes two problems:
the javadoc jar isn't excluded and will end up in lib beside the other jar,
nothing is found to be unpacked into docs and this makes the build fail.
Actually, the only way I found to get the whole stuff working is to use a pattern with a wildcard (more precisely *:javadoc). Below an updated assembly descriptor:
<assembly>
<id>bin</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<excludes>
<!-- Exclude the Jars that are included in later sections -->
<exclude>*:javadoc</exclude>
</excludes>
<outputDirectory>lib</outputDirectory>
<unpack>false</unpack>
<useTransitiveDependencies>true</useTransitiveDependencies>
<useTransitiveFiltering>false</useTransitiveFiltering>
<useProjectArtifact>false</useProjectArtifact>
</dependencySet>
<dependencySet>
<includes>
<include>*:javadoc</include>
</includes>
<outputDirectory>docs</outputDirectory>
<unpack>true</unpack>
<useTransitiveDependencies>true</useTransitiveDependencies>
<useProjectArtifact>false</useProjectArtifact>
<useStrictFiltering>true</useStrictFiltering>
</dependencySet>
</dependencySets>
</assembly>
Not sure this will be satisfying enough but at least, it's working and produces the expected result.
I managed to get it to work like this:
<?xml version="1.0" encoding="UTF-8"?>
<assembly>
<id>bundle</id>
<formats>
<format>zip</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<moduleSets>
<moduleSet>
<!-- Collect JAR libraries -->
<useAllReactorProjects>true</useAllReactorProjects>
<binaries>
<includeDependencies>true</includeDependencies>
<outputDirectory>/</outputDirectory>
<unpack>false</unpack>
</binaries>
</moduleSet>
<moduleSet>
<!-- Collect sources -->
<useAllReactorProjects>true</useAllReactorProjects>
<binaries>
<includeDependencies>true</includeDependencies>
<attachmentClassifier>sources</attachmentClassifier>
<outputDirectory>/</outputDirectory>
<unpack>false</unpack>
</binaries>
</moduleSet>
<moduleSet>
<!-- Collect javadoc -->
<useAllReactorProjects>true</useAllReactorProjects>
<binaries>
<includeDependencies>true</includeDependencies>
<attachmentClassifier>javadoc</attachmentClassifier>
<outputDirectory>/</outputDirectory>
<unpack>false</unpack>
</binaries>
</moduleSet>
</moduleSets>
Basically I have a moduleSet for the JARs, another for the sources and another for the javadocs.
The Assembly is made in the last module of the project.
Hope this helps.