Can't build with ant when invoked from maven - java

I have an ant build file that works when invoked by itself. When I invoke it from maven, the first few tasks execute fine (init, clean, etc.), but build fails with:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-antrun-plugin:1.7:run (compile) on project maven-stream: An Ant BuildException has occur
ed: The following error occurred while executing this line:
[ERROR] C:\maven_projects\cm\Qlarius Underwriter\build.xml:24: Unable to find a javac compiler;
[ERROR] com.sun.tools.javac.Main is not on the classpath.
[ERROR] Perhaps JAVA_HOME does not point to the JDK.
[ERROR] It is currently set to "C:\Program Files\Java\jdk1.7.0_07\jre"
[ERROR] around Ant part ...<ant antfile="C:\maven_projects\cm/Qlarius Underwriter/build.xml">... # 4:69 in C:\maven_projects\cm\target\antrun\build-ma
in.xml
Why would ant find java when invoked directly but not through maven?
The ant portion of the pom.xml file is:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<configuration>
<target>
<ant antfile="${basedir}/Qlarius Underwriter/build.xml">
<target name="LifeQuote"/>
</ant>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>

As the error suggests you're not pointing to a JDK. You need to change JAVA_HOME to be the root of your JDK and not the JRE. That way it will be able to find javac.
The other tasks init and clean will probably be ok if they are not using javac

Related

Cannot build any maven project with same error

It has been few weeks since I build anything in maven, and I just tried to build one of the projects and it gives me this error. I try with multiple project and they are throwing same error:
Execution default of goal org.codehaus.mojo:aspectj-maven-plugin:1.8:compile failed: Plugin org.codehaus.mojo:aspectj-maven-plugin:1.8 or one of its dependencies could not be resolved: Could not find artifact com.sun:tools:jar:15.0.2 at specified path /usr/local/Cellar/openjdk/15.0.2/libexec/openjdk.jdk/Contents/Home/../lib/tools.jar -> [Help 1]
How can I resolve this error? I can't even run mvn install -DskipTests or mvn package.
I am on JDK version 8.
Edit: here is my plugin from pom:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>${aspectj-maven-plugin.version}</version>
<configuration>
<complianceLevel>${java.version}</complianceLevel>
<source>${java.version}</source>
<target>${java.version}</target>
<showWeaveInfo>true</showWeaveInfo>
<verbose>true</verbose>
<aspectLibraries>
<dependency>
<groupId>com.jcabi</groupId>
<artifactId>jcabi-aspects</artifactId>
</dependency>
</aspectLibraries>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
java version = 1.8
aspectj-maven-plugin.version = 1.8
EDIT: I pull this project from the repo, which means it works on other people's computer/on jenkins. I believe this is something to do with my local computer. It also gives me same error if I use terminal.
I found the answer after more research. As #Michael Katt mentioned in the comment, my JAVA_HOME wasn't properly set up. When I did which java, it gave me /usr/bin/java, but when I did echo $JAVA_HOME, it gave me nothing.
So with help of other colleague, I did vi ~/.bash_profile and added export JAVA_HOME=$(/usr/libexec/java_home). Not exactly sure why, but it is something to do with matching it with my jdk in my project structure which has /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk...
I opened new shell and typed echo $JAVA_HOME and it gives me /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home. My project build/compiles now.
I am still not sure how $(/usr/libexec/java_home) translated to /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk...

build number maven plugin for Git repo

A project that was earlier on svn was recently moved to github after which i am facing this issue while creating a build. It fails to generate the build number with the below error
Failed to execute goal org.codehaus.mojo:buildnumber-maven-plugin:1.4:create (default) on project line-management: An error has occurred while checking scm status. Exception while executing SCM command. Error while executing command. Error while executing process. Cannot run program "svn" (in directory "D:\workspace\project_git\project_management"): CreateProcess error=2, The system cannot find the file specified -> [Help 1]
code of pom.xml
<scm>
<connection>scm:svn:http://some.svn.url/</connection>
</scm>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>buildnumber-maven-plugin</artifactId>
<version>1.4</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>create</goal>
</goals>
</execution>
</executions>
<configuration>
<doCheck>true</doCheck>
<doUpdate>false</doUpdate>
<timestampFormat>{0,date,"yyyyMMddHHmmss"}</timestampFormat>
<timestampPropertyName>buildNumVar</timestampPropertyName>
<shortRevisionLength>10</shortRevisionLength>
</configuration>
</plugin>
Your scm tag still contains the the connection string to your old svn repository.
You should provide the url of the new git repository here, for example <connection>scm:git:https://github.com/your-git-repo.git</connection>. For the details of the connection string, please check here.
When you install the svn program (tortoise for example) you need to check the command line option. By default, that option is disabled. That worked for me.

If I run a plugin from the command line at the root pom, what does it do when traversing modules?

I'm trying to use the plugin: http://gatling.io/docs/2.0.1/extensions/maven_plugin.html
I'm trying to run this command at the root pom:
mvn package gatling:execute
This multi module project only has this plugin defined in one child pom like so:
<plugins>
<plugin>
<groupId>io.gatling</groupId>
<artifactId>gatling-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>3.2.1</version>
<configuration>
<args>
<arg>-deprecation</arg>
<arg>-feature</arg>
<arg>-language:postfixOps</arg>
</args>
</configuration>
<executions>
<execution>
<id>scala-compile-first</id>
<phase>process-resources</phase>
<goals>
<goal>add-source</goal>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>scala-test-compile</id>
<phase>process-test-resources</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
But when I run this command at the root, it gives this error:
[INFO] ------------------------------------------------------------------------
[INFO] Building root-pom 2.5.210-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- gatling-maven-plugin:2.1.7:execute (default-cli) # root-pom ---
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at io.gatling.mojo.MainWithArgsInFile.runMain(MainWithArgsInFile.java:50)
at io.gatling.mojo.MainWithArgsInFile.main(MainWithArgsInFile.java:33)
Caused by: java.lang.RuntimeException: Can't find the jar matching (.*scala-library.*\.jar)$
at io.gatling.compiler.ZincCompiler$$anonfun$jarMatching$1$2.apply(ZincCompiler.scala:88)
at io.gatling.compiler.ZincCompiler$$anonfun$jarMatching$1$2.apply(ZincCompiler.scala:88)
at scala.Option.getOrElse(Option.scala:120)
at io.gatling.compiler.ZincCompiler$.jarMatching$1(ZincCompiler.scala:88)
at io.gatling.compiler.ZincCompiler$.setupZincCompiler(ZincCompiler.scala:91)
at io.gatling.compiler.ZincCompiler$delayedInit$body.apply(ZincCompiler.scala:106)
at scala.Function0$class.apply$mcV$sp(Function0.scala:40)
at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:12)
at scala.App$$anonfun$main$1.apply(App.scala:71)
at scala.App$$anonfun$main$1.apply(App.scala:71)
at scala.collection.immutable.List.foreach(List.scala:318)
at scala.collection.generic.TraversableForwarder$class.foreach(TraversableForwarder.scala:32)
at scala.App$class.main(App.scala:71)
at io.gatling.compiler.ZincCompiler$.main(ZincCompiler.scala:35)
at io.gatling.compiler.ZincCompiler.main(ZincCompiler.scala)
... 6 more
This is not a scala project, so this is definitely related to the plugin. Running mvn package without gatling:execute works fine.
I do not know enough about maven to be able to troubleshoot this problem because I can't really tell what maven is trying to do here. If it is trying to run gatling at the root, why? That plugin is not defined in the root pom anywhere. My question is:
When you run a plugin at the root of a multi-module project, how does it traverse my project, look for plugins and run them? I'm asking this question abstractly. You don't have to answer in terms of gatling. I just want to understand maven plugins better.
How do I troubleshoot this issue? I think I could add scala to the root pom, but since it would be the first dependency at that level, it seems like it's the wrong approach.
I could also cd into the one child pom that has this plugin and run the command there, but that means I'll only be running package on a piece of the project. Ideally I'd like to run package on the whole project before I run the plugin.
Running maven with a plugin goal like that will run that goal for all project modules, the same as mvn install will install each module. You don't have to define the plugin anywhere - if you specify it on the commandline, it is run.
You correctly bound the execution of that plugin to one or more lifecycle phases, in this case process-resources and process-test-resources. If you run mvn compile, the the process-resources phase will be executed and the add-sources and compile goals will be run for the gatling plugin. Similarly, running mvn test (which is also run when you mvn install) will run the testCompile goal of the gatling plugin.
What you'll want to do is to also bind the execute goal as is exemplified
on the page you linked. If you want everyting packaged first, bind it to the
integration test phase:
<execution>
<phase>integration-test</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
....
</configuration>
</execution>
Note that integration-test comes after package (and somewhere before install), so to run it, execute mvn integration-test or mvn install.

Maven: Can not create Javadocs

I can not create javadocs to a folder I like, Here is how my javadoc plugin looks like;
<build>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.9.1</version>
<configuration>
<outputDirectory>${build.dir}/resources/javadoc</outputDirectory>
<reportOutputDirectory>${project.reporting.outputDirectory}/javadoc</reportOutputDirectory>
</configuration>
<executions>
<execution>
<id>attach-javadocs</id>
<phase>package</phase>
<goals>
<goal>aggregate</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Any ideas?
EDIT MAven install leads to error"
Failed to execute goal org.apache.maven.plugins:maven-javadoc-plugin:2.9.1:aggregate
(attach-javadocs) on project priTool: An error has occurred in JavaDocs report generation:
[ERROR] Exit code: 1 - C:\pricing\priWebApp\src\main\..webapp\backend\local\PriUpgradeHelper.java:5:
package ..upgrademanager does not exist
[ERROR] import ...upgrademanager.ComponentInfo;
Upgrade Manager is an external dependency, but I can see its jar in library
You need to understand the difference between mvn package and mvn install. package creates the JARs in the target folder of the Maven project/module.
If there are more modules, they won't be able to see these JARs - for each project/module, Maven only looks into the local repository to resolve dependencies, even if could resolve them in the same project.
And that's what install does: Package the JARs and then install them into the local repository.
When you have a Maven project, with several modules (a.k.a a reactor build), then never use mvn package. Always use mvn install.

Maven Ant BuildException with maven-antrun-plugin ... unable to find javac compiler

I'm trying to make Maven call an ANT build for some legacy code. The ant build builds correctly through ant. However when I call it using the maven ant plugin, it fails with the following error:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-antrun-plugin:1.6:run (default) on project CoreServices: An Ant BuildException has occured: The following error occurred while executing this line:
[ERROR] C:\dev\projects\build\build.xml:158: The following error occurred while executing this line:
[ERROR] C:\dev\projects\build\build.xml:62: The following error occurred while executing this line:
[ERROR] C:\dev\projects\build\build.xml:33: The following error occurred while executing this line:
[ERROR] C:\dev\projects\ods\build.xml:41: Unable to find a javac compiler;
[ERROR] com.sun.tools.javac.Main is not on the classpath.
[ERROR] Perhaps JAVA_HOME does not point to the JDK.
[ERROR] It is currently set to "C:\bea\jdk150_11\jre"
My javac exists at C:\bea\jdk150_11\bin and this works for all other things. Im not sure where Maven is getting this version of JAVA_HOME. JAVA_HOME in windows environmental variables is set to C:\bea\jdk150_11\ as it should be.
The Maven code that I'm using to call the build.xml is
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<phase>install</phase>
<configuration>
<target>
<ant antfile="../build/build.xml" target="deliver" >
</ant>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
First thing: Why do you execute your ANT script in install phase and not in compile?
The second thing: Your problem may be caused by the fact that Maven executes JRE instead of JDK, despite the fact that your JAVA_HOME points to JDK. To fix this see you have to manually adjust dependencies for maven-antrun-plugin. This is just an example:
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.6</version>
<dependencies>
<dependency>
<groupId>com.sun</groupId>
<artifactId>tools</artifactId>
<version>1.5.0</version>
<scope>system</scope>
<systemPath>${java.home}/../lib/tools.jar</systemPath>
</dependency>
</dependencies>
<executions>
<execution>
<phase>compile</phase>
<configuration><target><ant/></target></configuration>
<goals><goal>run</goal></goals>
</execution>
</executions>
</plugin>

Categories