Maven project dependency "could not find artifact" - maven

I have 2 maven projects. Project A and Project B. Project B is dependent on Project A but not the other way around.
In my project B pom I have this:
<dependency>
<groupId>com</groupId>
<artifactId>ProjectA</artifactId>
<type>pom</type>
<version>1.0-SNAPSHOT</version>
</dependency>
When i try to package the project it fails with this error:
[ERROR] Failed to execute goal on project ProjectB: Could not resolve dependencies for project com:ProjectB:war:1.0-SNAPSHOT: Could not find artifact com:ProjectA:pom:1.0-SNAPSHOT -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal on project ProjectB: Could not resolve dependencies for project com:ProjectB:war:1.0-SNAPSHOT: Could not find artifact com:ProjectA:pom:1.0-SNAPSHOT
So it can't find my ProjectA pom. Do I need to put it in my project? Where should it be located in my file structure.
For what it's worth, I am using intelliJ IDE.
Thanks in advance.
EDIT: When i run install on projectA I get this error:
The packaging for this project did not assign a file to the build artifact
EDIT2 - Adding ProjectA 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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>edu</groupId>
<artifactId>ProjectA</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>ProjectA</name>
<build>
<sourceDirectory>src/main/java</sourceDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
<executions>
<execution>
<id>install-jar-lib</id>
<phase>validate</phase>
<goals>
<goal>install-file</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<id>prepare-package</id>
<phase>prepare-package</phase>
<configuration>
<target>
<copy file="${basedir}/src/main/webapp/META-INF/context-${app.environment}.xml"
tofile="${basedir}/src/main/webapp/META-INF/context.xml" />
<copy file="${basedir}/src/main/webapp/WEB-INF/web-${app.environment}.xml"
tofile="${basedir}/src/main/webapp/WEB-INF/web.xml" />
<copy file="${basedir}/src/main/resources/log4j-${app.environment}.xml"
tofile="${basedir}/src/main/resources/log4j.xml" />
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
<execution>
<phase>compile</phase>
<configuration>
<target>
<copy file="${project.build.directory}/classes/log4j-${app.environment}.xml"
tofile="${project.build.directory}/classes/log4j.xml" />
<delete>
<fileset dir="${project.build.directory}/classes" includes="**/*-local.*"/>
<fileset dir="${project.build.directory}/classes" includes="**/*-test.*"/>
<fileset dir="${project.build.directory}/classes" includes="**/*-prod.*"/>
</delete>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
<execution>
<id>package</id>
<phase>package</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<delete file="${basedir}/src/main/webapp/META-INF/context.xml" />
<delete file="${basedir}/src/main/webapp/WEB-INF/web.xml" />
<tstamp>
<format property="last.timestamp" pattern="yyyyMMddHHmmss"/>
</tstamp>
<property name="build.time" value="${last.timestamp}" />
</target>
<exportAntProperties>true</exportAntProperties>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<failOnMissingWebXml>true</failOnMissingWebXml>
<!--<webResources>
<resource>
<directory>src/main/WEB-INF/lib</directory>
<targetPath>WEB-INF/lib</targetPath>
<filtering>false</filtering>
<includes>
<include>**/*.jar</include>
</includes>
</resource>
</webResources>-->
</configuration>
</plugin>
<plugin>
<groupId>com.coderplus.maven.plugins</groupId>
<artifactId>copy-rename-maven-plugin</artifactId>
<version>1.0.1</version>
<executions>
<execution>
<id>rename-file</id>
<phase>package</phase>
<goals>
<goal>rename</goal>
</goals>
<configuration>
<sourceFile>${project.build.directory}/${artifactId}-${version}.war</sourceFile>
<destinationFile>${project.build.directory}/ProjectA##${build.time}.war</destinationFile>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

You have to install ProjectA in your local Maven repo (or otherwise make it available in whatever remote repositories your pom.xml or settings.xml point at). For example:
cd whereever/projectA/lives
mvn clean install
This will write com/ProjectA/ProjectA.pom to your local Maven repo and when you do this ...
cd wherever/projectB/lives
mvn clean install
... Maven will resolve ProjectA.pom from that location.
Note: the dependency you have declared on ProjectA:
<dependency>
<groupId>com</groupId>
<artifactId>ProjectA</artifactId>
<type>pom</type>
<version>1.0-SNAPSHOT</version>
</dependency>
... will transitively add all dependencies declared in com:ProjectA to ProjectB's POM, is that definitely your intention? This only makes sense if ProjectA is packaged as a POM, if it is packaged as a JAR then you need to update the dependency declaration in ProjectB to remove this line: <type>pom</type>.

Related

Maven Antrun Plugin inheritance

I have a maven multi module project structure.
I want to build the jar of a child project using maven-antrun-plugin by declaring it inside the parent project's pom.
Here is my parent-pom:
<groupId>com.package.abc</groupId>
<artifactId>parent-pom</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<id>concat-build-classpath</id>
<phase>package</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<jar destfile="${project.build.directory}/${project.build.finalName}.jar"
basedir="src/main">
<manifest>
<attribute name="Main-Class" value="com.package.abc.Application" />
</manifest>
</jar>
</target>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<id>default-jar</id>
<phase>never</phase>
<configuration>
<finalName>unwanted</finalName>
<classifier>unwanted</classifier>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build></project>
and here is my child project's pom:
<modelVersion>4.0.0</modelVersion>
<artifactId>sample-project</artifactId>
<parent>
<groupId>com.package.abc</groupId>
<artifactId>parent-pom</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<build>
<finalName>sample-project</finalName>
</build></project>
When I am building child project it shows the error as:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-install-plugin:2.4:install (default-install) on project sample-project: The packaging for this project did not assign a file to the build artifact -> [Help 1]
You can set <inherited> to false in <plugin> element:
...
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.5</version>
<inherited>false</inherited>
...
More info at:
https://maven.apache.org/pom.html#plugins
https://maven.apache.org/guides/mini/guide-configuring-plugins.html#Using_the_inherited_Tag_In_Build_Plugins

How to make Maven Clean Install build JavaFX8?

Every time I try to run my JavaFX 8 project, I need to do a clean install with Maven then build project with eclipse. Doing only the clean install will result in an Exception.
How can I make the maven clean install and eclipse build execute in one command.
If I need to add it in my pom.xml, should I put it in my parent pom.xml or all in its children?
Thanks!
Finally got mine working, I just added this lines of code on my parent pom.xml.
<build>
<plugins>
<!-- This will copy all your dependencies to target/libs, which will be
picked up by the ant task below -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/libs</outputDirectory>
<includeScope>compile</includeScope>
<includeScope>runtime</includeScope>
<excludeArtifactIds>javafx</excludeArtifactIds>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>create-javafx-packages</id>
<phase>package</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target xmlns:fx="javafx:com.sun.javafx.tools.ant">
<taskdef uri="javafx:com.sun.javafx.tools.ant" resource="com/sun/javafx/tools/ant/antlib.xml" classpath="${javafx.tools.ant.jar}"/>
<fx:application id="fxApp" name="${project.name}" mainClass="${project.main.class}"/>
<!-- Note: this will overwrite the JAR produced by maven-jar-plugin,
change destfile if you don't want this -->
<fx:jar destfile="${project.build.directory}/../../${project.build.finalName}">
<fx:application refid="fxApp"/>
<fx:fileset dir="${project.build.directory}/../.." includes="target/classes"/>
<fx:resources>
<fx:fileset dir="${project.build.directory}/../.." includes="libs/*.jar"/>
</fx:resources>
</fx:jar>
<fx:deploy outdir="${project.build.directory}/../../javafx-output" outfile="${project.build.finalName}" nativeBundles="all">
<fx:application refid="fxApp"/>
<fx:resources>
<!-- If you changed <fx:jar> above, don't forget to modify the
line below -->
<fx:fileset dir="${project.build.directory}/../.." includes="${project.build.finalName}.jar"/>
<fx:fileset dir="${project.build.directory}/../.." includes="libs/*.jar"/>
</fx:resources>
</fx:deploy>
</target>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>javafx</artifactId>
<version>${javafx.version}</version>
<scope>system</scope>
<systemPath>${java.home}/lib/ext/jfxrt.jar</systemPath>
</dependency>
</dependencies>
Try doing the following
mvn clean install eclipse:clean eclipse:eclipse and then do a refresh on the project in eclipse.
HTH,
Keshava.

Maven build sub projects using root pom

I have different independent maven projects and want to build it with single pom. So I am using following 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>a.b</groupId>
<artifactId>my-sdk</artifactId>
<name>my-sdk</name>
<version>1.0</version>
<packaging>pom</packaging>
<description>Generating SDK</description>
<modules>
<module>project1</module>
<module>project2</module>
<module>project3</module>
</modules>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.9</version>
<executions>
<execution>
<id>process-sources</id>
<phase>process-classes</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>process-classes</phase>
<configuration>
<tasks>
<ant antfile="build.xml" target="init" />
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>jboss-repo</id>
<url>http://repository.jboss.org/maven2</url>
</repository>
</repositories>
I have the following questions:-
Javadoc is not generating, so where I am doing wrong. If I run "mvn javadoc:javadoc package" command it generates java doc. But if I run "mvn package" it doesn't.
In project2 there is assembly plugin to generate fat jar, it generates jar of project2 but unable to generate fat jar. If I build project2 independently it generate fat jar successfully.
Follwing section is from project2 pom to generate fat jar which works successfully if I run the build independently
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<configuration>
<archive>
<manifest>
<mainClass>[MAIN CLASS]</mainClass>
</manifest>
<manifestEntries>
<Class-Path>.</Class-Path>
</manifestEntries>
</archive>
</configuration>
<goals>
<goal>assembly</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Any Suggestions.
I'd say check the documentation for both of your questions ;-)
javadoc (http://maven.apache.org/plugins/maven-javadoc-plugin/plugin-info.html)
javadoc:jar and javadoc:javadoc should not be used for an aggregator project, use javadoc:aggregate or (in your case) javadoc:aggregate-jar
assembly (http://maven.apache.org/plugins/maven-assembly-plugin/assembly-mojo.html)
first, assembly:assembly is deprecated and second, it should only be used on the command line. I guess using goal single will help.

Set directory in Maven Antrun Plugin

I've downloaded jquery-ui to my webapp which has a build.xml for compressing and minifying.
Now I would like to run this build.xml from within my pom.xml:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<configuration>
<target>
<ant antfile="src/main/webapp/resources/js/jquery-ui/build/build.xml" />
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
But now the behavior for this buildfile is wrong. It creates a dist folder on the wrong place. Here's thebuild.xml` from jquery-ui: https://github.com/jquery/jquery-ui/blob/master/build/build.xml
It creates the dist folder on the same place where pom.xml is (same place i run mvn clean package) and it should create it in src/main/webapp/resources/js/jquery-ui/build
Is it possible to run build.xml from a specified directory (working directory)?
Can't access the documentation for the Ant task: http://ant.apache.org/manual/CoreTasks/ant.html
EDIT: pom.xml
<?xml version="1.0" encoding="utf-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example.myproject</groupId>
<artifactId>myproject</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>myproject</name>
<dependencies>
<!-- ... -->
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<!-- ... -->
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<warName>myproject</warName>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>install</id>
<phase>install</phase>
<goals>
<goal>sources</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<executions>
<execution>
<phase>compile</phase>
<configuration>
<executable>make</executable>
<workingDirectory>src/main/webapp/resources/js/jquery</workingDirectory>
</configuration>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>compile</phase>
<configuration>
<target>
<ant antfile="build.xml" dir="src/main/webapp/resources/js/jquery-ui/build" />
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
On running mvn clean package I'm getting the following errors in minify target of jquery-ui's build.xml:
[apply] build/minify-js.sh: Line 3: /home/danny/myproject/dist/jquery-ui-1.9pre/ui/minified/jquery-ui.min.js: File or directory not found
The correct path should be ${basedir}/src/main/webapp/resources/js/jquery-ui/build/dist/jquery-ui-1.9pre/ui/minified/jquery-ui.min.js instead of the above mentioned...
According to http://ant.apache.org/manual/Tasks/ant.html, you can use the dir attribute:
<mkdir dir="${project.build.directory}/jquery-ui" />
<ant antfile="src/main/webapp/resources/js/jquery-ui/build/build.xml" dir="${project.build.directory}/jquery-ui" />
On the other hand, are you really sure you want to create the build directory underneath your src directory? Shouldn't this go into target rather?
I would recommend to use the maven plugin instead of Ant calls. Based on the docs maven-minify-plugin to do what you need. The plugin can be found in Maven Central. And furthermore you will find the maven-plugin-documention how to use the maven-plugin.

Adding java source (.java files) to test jar in Maven

I'm making use of my pom.xml and am was able to generate the jar for src/main/java (say app.jar) as well as for src/test/java (say app-test.jar). I was also able to include my java sources as part of the app.jar (i.e. have both my .class as well as my .java files in the jar).
However for my app-test.jar, i'm not able to include my .java files in it.
This is 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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>my-app</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>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/java</directory>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>test-jar</goal>
</goals>
<configuration>
<includes>
<include>src/test/java</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Any help would be appreciated.
Thanks.
Update on post on Whaley's suggestion:
Tried the maven-antrun-plugin, but rt now after running mvn package all i'm getting inside my tests.jar is the META-INF folder. .java and .class are not getting included:
This is the part of the pom.xml
<build>
<resources>
<resource>
<directory>src/main/java</directory>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>test-jar</goal>
</goals>
<configuration>
<includes>
<include>src/test/java</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>${project.artifactId}-include-sources</id>
<phase>process-resources</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<copy todir="${project.build.testOutputDirectory}">
<fileset dir="${project.build.testSourceDirectory}"/>
</copy>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Thanks.
I think you probably need to generate a custom assembly using the assembly plugin:
http://maven.apache.org/plugins/maven-assembly-plugin/single-mojo.html
But a much cleaner solution would be to package test sources separately using the source plugin
http://maven.apache.org/plugins/maven-source-plugin/test-jar-mojo.html
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.1.2</version>
<executions>
<execution>
<id>attach-test-sources</id>
<goals>
<goal>test-jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
you can then have both the test source jar and the test class jar on your classpath and do something like this:
YourClassName.class.getResource("YourClassName.java");
or more generically:
public static InputStream getSourceForClass(final Class<?> clazz) {
final String baseName = clazz.getSimpleName();
return clazz.getResourceAsStream(baseName + ".java");
}
to access the sources
The includes property of the jar plugin only allows the inclusion of files from a relative path from the plugin's classesDirectory property. So that won't work unless you copied your .java files to ${project.build.outputDirectory} somehow. You could do something like that with the maven-antrun-plugin:
<build>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>${project.artifactId}-include-sources</id>
<phase>process-resources</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<copy todir="${project.build.outputDirectory}">
<fileset dir="${project.build.SourceDirectory}"/>
</copy>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

Resources