How to trigger a Tomcat source build using maven? - maven

in my project I would like to compile tomcat 10.0.14 sources from pom.xml of my project. How can I run ant to compile tomcat sources inside maven for my purpose?
I tried with maven-antrun-plugin
<?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>it.atlantica</groupId>
<artifactId>tomcat</artifactId>
<version>10.0.14</version>
<packaging>pom</packaging>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>build-tomcat</id>
<phase>package</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<ant antfile="${basedir}/build.xml" target="deploy"/>
</target>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
but I have this error:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-antrun-plugin:3.0.0:run (build-tomcat) on project tomcat: An Ant BuildException has occured: The following error occurred while executing this line:
[ERROR] /root/sam/tomcat-10.0.14/build.xml:978: 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 "/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.312.b07-2.el8_5.x86_64/jre"
[ERROR] around Ant part ...<ant antfile="/root/sam/tomcat-10.0.14/build.xml" target="deploy" />... # 4:73 in /root/sam/tomcat-10.0.14/target/antrun/build-main.xml
If I set JAVA_HOME, instead I have other errors:
/root/sam/tomcat-10.0.14/java/org/apache/catalina/ant/AbstractCatalinaCommandTask.java:22: error: package org.apache.tools.ant does not exist
import org.apache.tools.ant.BuildException;
^
/root/sam/tomcat-10.0.14/java/org/apache/catalina/ant/BaseRedirectorHelperTask.java:47: error: cannot find symbol
public abstract class BaseRedirectorHelperTask extends Task {
/root/sam/tomcat-10.0.14/java/org/apache/catalina/ant/AbstractCatalinaCommandTask.java:64: error: cannot find symbol
public StringBuilder createQueryString(String command) throws BuildException {
^
Thanks,
Marco

Related

get error when i try to consume spec openapi in service project backbase

I'm following A tutorial in backbase academy exactly in chapter "Backbase Backend Essentials Course - Service SDK and Direct Integration Services" and i get error when i try to consume specification from service project , this is the error
exchange-rate-service: Compilation failure: Compilation failure:
symbol: method requiredMode()
[ERROR] location: #interface Schema
and this is my pom.xml of spec project
<?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>
<parent>
<groupId>com.backbase.buildingblocks</groupId>
<artifactId>backbase-openapi-spec-starter-parent</artifactId>
<version>11.3.0</version>
</parent>
<groupId>com.backbase.training</groupId>
<artifactId>exchange-rate-specification</artifactId>
<version>1.0.0-SNAPSHOT</version>
<name>Backbase :: Exchange Rate Specification</name>
<properties>
<!-- plugin versions -->
<boat-maven-plugin.version>0.4.0</boat-maven-plugin.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>com.backbase.oss</groupId>
<artifactId>boat-maven-plugin</artifactId>
<version>${boat-maven-plugin.version}</version>
<executions>
<execution>
<id>boat-validation</id>
<phase>generate-sources</phase>
<goals>
<goal>validate</goal>
</goals>
<configuration>
<input>${project.basedir}/src/main/resources/openapi.yaml</input>
<failOnWarning>true</failOnWarning>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
even with this project i get the same error
https://github.com/Backbase/documentation-code-samples/tree/master/service-sdk/11.0.0/create-openapi-spec-project
these is the steps that i followed :
1.download project from github
2.run cmd "mvn clean install" on project "messaging-api"
until now i get BUILD SUCCESS
3. run cmd "mvn clean install" on project "messaging-service"
i get this error
ErrorItem.java:[67,3] error: cannot find symbol
symbol: class Schema
location: class ErrorItem

How does maven decide when to use the target folder for classpath

I have a question regarding how maven calculates the classpath during building. Specifically, what controls when the "target/classes" is used and when the "jar" from a repository (local/remote) is used.
I have a project on version 1.0.0-SNAPSHOT where the artifacts have NOT been installed/deployed so there is no "jar" in some repository (remote or local) to resolve them. I want to run "generate-sources" WITHOUT installing locally (no 'mvn install' run).
The structure looks like this:
parent-prj
parent-prj/sub-prj
parent-prj/gen-src-prj <--- This depends on 'sub-prj'
When I run "mvn -am -pl parent-prj/gen-src-prj generate-sources" in order to just generate some java files, it does not work:
[ERROR] Failed to execute goal on project gen-src-prj: Could
not resolve dependencies for project
mygrp:gen-src-prj:jar:1.0.0-SNAPSHOT:
Could not find artifact
mygrp:sub-prj:jar:1.0.0-SNAPSHOT -> [Help 1]
Using debug output and adding "dependency:build-classpath" I can confirm that maven ignores the presence of "sub-prj" in the reactor and looks for a "jar" somewhere which it can't find. Yet the project is printed in the reactor summary:
[INFO] Reactor Summary:
[INFO]
[INFO] parent-prj ..................................... SUCCESS [ 0.625 s]
[INFO] sub-prj ........................................ SUCCESS [ 0.018 s]
[INFO] gen-src-prj .................................... FAILURE [ 0.040 s]
The interesting thing I noticed is that running the compile goal works fine! This uses sub-prj/target/classes (as shown by dependency:build-classpath) and has no trouble generating the sources and even compiling them: "mvn -am -pl parent-prj/gen-src-prj compile"
So here are the points I want to understand:
Why does the compile goal work but the generate-sources doesn't work?
At what point does maven decide to use the output folder of previous projects on the reactor classpath instead of looking for a jar?
Is there a way for generate-sources to run directly as I want it EVEN WITHOUT having its dependencies resolved?
Regarding (3) my generation tool is a utility invoked by:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
The tool reads some XML in src/main/resources and generates Java files and does NOT need anything in its class-path (so there is no need for maven to resolve it).
Also note that I would be interested to understand (1) and (2) even if a solution for (3) is provided.
EDIT: Per comment request, adding full example
parent-prj/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>mygrp</groupId>
<artifactId>parent-prj</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>sub-prj</module>
<module>gen-src-prj</module>
</modules>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.9</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
parent-prj/sub-prj/pom.xml
<?xml version="1.0"?>
<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>
<parent>
<groupId>mygrp</groupId>
<artifactId>parent-prj</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<artifactId>sub-prj</artifactId>
</project>
parent-prj/gen-src-prj/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>
<parent>
<groupId>mygrp</groupId>
<artifactId>parent-prj</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<artifactId>gen-src-prj</artifactId>
<dependencies>
<dependency>
<groupId>mygrp</groupId>
<artifactId>sub-prj</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<includeProjectDependencies>false</includeProjectDependencies>
<includePluginDependencies>true</includePluginDependencies>
<mainClass>uk.co.real_logic.sbe.SbeTool</mainClass>
<systemProperties>
<systemProperty>
<key>sbe.output.dir</key>
<value>${project.build.directory}/generated-sources/java</value>
</systemProperty>
<systemProperty>
<key>sbe.validation.warnings.fatal</key>
<value>true</value>
</systemProperty>
</systemProperties>
<arguments>
<argument>${project.build.resources[0].directory}/Examples.xml</argument>
</arguments>
<workingDirectory>${project.build.directory}/generated-sources/java</workingDirectory>
</configuration>
<dependencies>
<dependency>
<groupId>uk.co.real-logic</groupId>
<artifactId>sbe-tool</artifactId>
<version>1.7.10</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>target/generated-sources/java/</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
EDIT: Armed with the knowledge from the answers I have come up with this workaround that allows one to achieve the desired behaviour. I list the dependencies in a profile that is active by default, then use another profile to run generate-sources with no dependencies active, like follows:
parent-prj/gen-src-prj/pom.xml
<profiles>
<profile>
<id>default</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<dependencies>
<dependency>
<groupId>mygrp</groupId>
<artifactId>sub-prj</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</profile>
<profile>
<id>excludeDependency</id>
<dependencies>
</dependencies>
</profile>
</profiles>
To generate sources with above, use: mvn -PexcludeDependency generate-sources
Maven can reference only output generated in current Session (during currently executing shell command). It uses the most "mature" place to look for the "output":
If compile is run - the classes end up in the target/classes dir, thus other modules can reference that
If package is run - then target/*.jar is created and this jar file ends up in the classpath instead
If install is run - then jar file ends up in the local repository - which is what ends up on the classpath
So there are 3 factors that impede your task:
maven-exec-plugin requires dependency resolution (as pointed out by #mondaka)
Your module1 references module2
generate-sources is run before the compilation. Thus module2 is not yet prepared to be used as a dependency.
So if you want to do it your way - you'll have to run at least compile phase each time you use anything from the Default Lifecycle. Or you could write your own plugin that doesn't require dependency resolution.
This problem is related to an open maven bug:
https://issues.apache.org/jira/browse/MNG-3283
The issue says: "The problem only occurs when a plugin binds itself to the
generate-sources phase and has #requiresDependencyResolution".
I have checked that exec-maven-plugin Mojo have indeed requiresDependencyResolution = ResolutionScope.TEST. You can see that on https://github.com/mojohaus/exec-maven-plugin/blob/master/src/main/java/org/codehaus/mojo/exec/ExecJavaMojo.java
Then, your only option is to use compile or process-classes phases. This is a Major open bug from 2007...

Maven duplicate class with generated sources when using -f option

I try to build a Maven project with classes generated from jaxb2-maven-plugin.
Consider the following minimal 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>test</groupId>
<artifactId>test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<version>2.3.1</version>
<executions>
<execution>
<goals>
<goal>xjc</goal>
</goals>
<configuration>
<packageName>test</packageName>
<sources>
<source>src/main/resources/schema.xsd</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
And this minimal schema in src/main/resources/schema.xsd:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema elementFormDefault="qualified" vc:maxVersion="1.1" vc:minVersion="1.0" xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="root" type="xs:string"/>
</xs:schema>
When invoking mvn clean compile, the project is built as expected. But when I specify the file using a path containing .., the following error occurs. When using a (relative or absolute) path without .., everything is fine.
C:\dev\test>mvn -f ..\test\pom.xml clean compile
[...]
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project test: Compilation failure: Compilation failure:
[ERROR] /C:/dev/test/target/generated-sources/jaxb/test/ObjectFactory.java:[32,8] duplicate class: test.ObjectFactory
How can I get rid of this error and build my project using mvn -f <some path containing "..">?
For me (on macOS), the issue goes away when I don't have a "." component in my path:
maven compile -f ./blah/blah # <-- "duplicate class" errors
maven compile -f blah/blah # <-- works fine
Ugly workaround: Try using an absolute path?
(Seems like a path normalization bug in Maven or maven-compiler-plugin.)

maven-enforcer-plugin and child pom

I am trying to use the maven-enforcer-plugin to ensure that one profile is activated (requireActiveProfile rule). See the second example on this page: Require Active Profile
It works well with a single pom file. When I try to use it with 2 pom files (a parent and a child) this do not work anymore when I try to build the child module.
I have any idea why it doesn’t work?
Complete example:
EXAMPLE
| pom.xml <1> (parent pom)
|
\---child
pom.xml <2> (child pom)
Parent pom file <1>:
<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.parent</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<profiles>
<profile>
<id>first</id>
<properties>
<my-name>Alice</my-name>
</properties>
</profile>
<profile>
<id>second</id>
<properties>
<my-name>Bob</my-name>
</properties>
</profile>
</profiles>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>1.4.1</version>
<executions>
<execution>
<id>enforce-first-or-second-profile-is-active</id>
<goals>
<goal>enforce</goal>
</goals>
<configuration>
<rules>
<requireActiveProfile>
<profiles>first,second</profiles>
<all>false</all>
</requireActiveProfile>
</rules>
<fail>true</fail>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<echo>Hello ${my-name}!</echo>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Child pom file <2>:
<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.mycompany.app</groupId>
<artifactId>my-app.parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>my-app</artifactId>
<packaging>pom</packaging>
</project>
Now I run:
EXAMPLE>cd child
EXAMPLE\child>mvn compile -Psecond
And the output looks like that:
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building my-app 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-enforcer-plugin:1.4.1:enforce (enforce-first-or-second-profile-is-active) # my-app ---
[WARNING] Rule 0: org.apache.maven.plugins.enforcer.RequireActiveProfile failed with message:
Profile "first" is not activated.
Profile "second" is not activated.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.178 s
[INFO] Finished at: 2015-10-30T18:03:50+01:00
[INFO] Final Memory: 24M/989M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-enforcer-plugin:1.4.1:enforce (enforce-first-or-second-profile-is-active) on project my-app: Some Enforcer rules have failed. Look above for specific messages explaining why the rule failed. -> [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
When I run maven on the parent pom, it works. What did I miss?
Maven Profiles are a bit confusing, but I'll venture an explanation based on my experiences.
The profile definitions is not inherited, but the effects of them are.
If you jump to the child directory and run mvn -Pfirst help:active-profiles you'll see that the effect of the parent's profile is indeed there:
Part of the output:
...
<properties>
<my-name>Alice</my-name>
</properties>
...
but you won't see the profiles definition in there.
The bad news is that your enforcer rule won't work in this scenario, unless every child also defines the required profiles.
The good news is that, armed with this new knowledge, you can use another enforcer rule (requireProperty) to achieve what you want:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>1.4.1</version>
<executions>
<execution>
<id>enforce-property</id>
<goals>
<goal>enforce</goal>
</goals>
<configuration>
<rules>
<requireProperty>
<property>my-name</property>
<message>You must set a my-name property! Did you activate the right profile?</message>
</requireProperty>
</rules>
<fail>true</fail>
</configuration>
</execution>
</executions>
</plugin>

HOw to run Jmeter tests with Maven?

Can someone suggest how to run Jmeter tests with Maven please? This is just a simple jmeter script with http call to google.com with no variables. Just want to start with a simple one.
I've created the path C:\Jmeterautomation\src\test\jmeter\google.jmx that maven recognizes
I've created pom.xml in this directory C:\Jmeterautomation
This is what pom looks like
<?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.adp.ds.services</groupId>
<artifactId>01-run-with-Maven</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<build>
<plugins>
<plugin>
<groupId>com.lazerycode.jmeter</groupId>
<artifactId>jmeter-Maven-plugin</artifactId>
<version>1.4.1</version>
<executions>
<execution>
<id>jmeter-tests</id>
<phase>verify</phase>
<goals>
<goal>jmeter</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
when I open command window, and go to this C:\Jmeterautomation and type mvn verify, I'm getting error mvn is not recognized as internal or external command
I've download this jmeter-maven-plugin-1.4.1.jar and kept in C:\Jmeter\apache-jmeter-2.13\lib\ext.
what am I doing wrong here?Tried all possible ways that I know off,Is there any step by step instructions that can go through the process, please suggest.
Thanks in advance
I'm getting error mvn is not recognized as internal or external command
Please install maven first:
https://maven.apache.org/guides/getting-started/windows-prerequisites.html

Resources