Maven errors to get a property from another pom - java

I have 3 pom.xml,
In one pom I have the profiles for test/prod.., depending on which one is active there are 2 folder it could be choose, the problem is when I update maven I get this error:
on project projWeb: Execution default-war of goal org.apache.maven.plugins:maven-war-plugin:3.2.2:war fai
led: basedir C:\Progetti\Proj\sviluppo\archicon\projWeb\src\main\webapp\WEB-INF\jbossWeb\${current.jbossWeb} does not exist -
in the parent pom I have this: C:\Progetti\Proj\sviluppo\proj\pom.xml
<modelVersion>4.0.0</modelVersion>
<groupId>it.sisal</groupId>
<artifactId>proj</artifactId>
<version>${appVersion}</version>
<packaging>pom</packaging>
<name>Proj</name>
<modules>
<module>projWeb</module>
<module>projEAR</module>
</modules>
....
<profile>
<id>test</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<properties>
<environment.profile>test</environment.profile>
<current.jbossWeb>securityOff</current.jbossWeb>
</properties>
</profile>
In my child pom.xml: C:\Progetti\Proj\sviluppo\proj\projWeb\pom.xml
<modelVersion>4.0.0</modelVersion>
<groupId>it.sisal</groupId>
<artifactId>projWeb</artifactId>
<packaging>war</packaging>
<version>0.0.40-SNAPSHOT</version>
<name>Proj Web</name>
<parent>
<artifactId>proj</artifactId>
<groupId>it.sisal</groupId>
<version>${appVersion}</version>
</parent>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<!-- <warSourceDirectory>${project.basedir}/WebContent</warSourceDirectory> -->
<webXml>${current.webXml}</webXml>
<failOnMissingWebXml>false</failOnMissingWebXml>
<!-- <warSourceExcludes>css/</warSourceExcludes> -->
<webResources>
<resource>
<directory>${jbossWeb.folder}/${current.jbossWeb}</directory>
<targetPath>WEB-INF</targetPath>
</resource>
</webResources>
</configuration>
</plugin>
Even in Intellij I can see:
Can Somebody could explain me why Can't I get the property and how could I solve it, please.

Related

Eclipse maven exception java.lang.ExceptionInInitializerError: Cannot access defaults field of Properties [in thread "Worker-27: Building"]

I have just updated eclipse to Version: 2022-06 (4.24.0), and have the next exception in problems view (in pom.xml file):
Exception java.lang.ExceptionInInitializerError: Cannot access defaults field of Properties [in thread "Worker-27: Building"]
The pom.xml file contains:
<?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>
...
</parent>
<artifactId>xxxx</artifactId>
<packaging>war</packaging>
<properties>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
...
</dependencies>
<repositories>
<repository>
...
</repository>
</repositories>
<build>
<finalName>xxxx</finalName>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<webXml>WebContent\WEB-INF\web.xml</webXml>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
No problems when generating maven artifactory.
Any idea how to avoid these error?
the solution is modify maven-war-plugin adding version 3.3.2:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.3.2</version>
<configuration>
<webXml>WebContent\WEB-INF\web.xml</webXml>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
thanks

Building Maven project doesn't create a jar?

So, I tried making another project depending on my base project, it worked and now for some reason my base project won't build correctly.
Whenever I build my maven project, no jar is outputted anywhere. I checked my build path, and the target folder.
My 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>...</groupId>
<artifactId>...</artifactId>
<version>1.1</version>
<repositories>
...
</repositories>
<dependencies>
...
</dependencies>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<resources>
<resource>
<directory>${basedir}</directory>
<includes>
...
</includes>
<excludes>
<exclude>target/</exclude>
</excludes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.1</version>
<configuration>
<outputDirectory>...</outputDirectory>
</configuration>
</plugin>
</plugins>
</build>
</project>
Now, with my original builds they still had the target folder for some reason, but now nothing is built at all.

spring boot package does not exist error

I'm compiling my project with mvn clean package, and failed with package does not exist.
The detail command:
Get the jar file target/xxxx.jar by running mvn clean package in source project.
install this jar file by running mvn install:install-file -Dfile=lib/xxxx.jar -DgroupId=com.company -DartifactId=source-package-name -Dversion=1.0.0 -Dpackaging=jar
add code to target project, which will use some functions in source project
compile target project by running mvn clean package, and it just failed with package does not exist
here is the source project 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.company</groupId>
<artifactId>source-package-name</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<name>source-package-name</name>
<description>xxxx</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>-Xmx6144m</argLine>
</configuration>
</plugin>
</plugins>
</build>
</project>
here is the target project 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.company</groupId>
<artifactId>target-package-name</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>target-package-name</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jdk.version>1.7</jdk.version>
<smartv-common.version>0.3.5s</smartv-common.version>
<spring.version>3.0.5.RELEASE</spring.version>
</properties>
<dependencies>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<excludes>
<!-- Exclude those since they are copied from the profile folder for
the build -->
<exclude>system.properties</exclude>
</excludes>
<filtering>false</filtering>
</resource>
</resources>
<finalName>xxxxx</finalName>
<!-- Set a compiler level -->
<extensions>
<extension>
<groupId>kr.motd.maven</groupId>
<artifactId>os-maven-plugin</artifactId>
<version>1.4.1.Final</version>
</extension>
</extensions>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>${jdk.version}</source>
<target>${jdk.version}</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<!-- http://maven.apache.org/plugins/maven-war-plugin/examples/adding-filtering-webresources.html
http://stackoverflow.com/questions/12729513/how-to-overwrite-files-in-the-war-file-during-maven-build -->
<webResources>
<!-- Resources from the activated profile folder -->
<resource>
<targetPath>WEB-INF/classes/</targetPath>
<includes>
<include>system.properties</include>
</includes>
</resource>
</webResources>
</configuration>
</plugin>
</plugins>
</build>
</project>
Ideally, you shouldn't use a Spring Boot application (something that's been repackaged) as a dependency. From the documentation:
Like a war file, a Spring Boot application is not intended to be used as a dependency. If your application contains classes that you want to share with other projects, the recommended approach is to move that code into a separate module. The separate module can then be depended upon by your application and other projects.
If the proposed solution isn't possible in your situation, the documentation goes on to describe an alternative:
If you cannot rearrange your code as recommended above, Spring Boot’s Maven and Gradle plugins must be configured to produce a separate artifact that is suitable for use as a dependency. The executable archive cannot be used as a dependency as the executable jar format packages application classes in BOOT-INF/classes. This means that they cannot be found when the executable jar is used as a dependency.
To produce the two artifacts, one that can be used as a dependency and one that is executable, a classifier must be specified. This classifier is applied to the name of the executable archive, leaving the default archive for use as dependency.
To configure a classifier of exec in Maven, the following configuration can be used:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<classifier>exec</classifier>
</configuration>
</plugin>
</plugins>
</build>

Maven TestNG project, Replace value in properties file with a Maven command line argument

I have a Maven TestNG project, that has a properties file located in src/test/resources. I wanted to parameterize certain values in the properties file such that they come from the Maven command line arguments (i,e mvn clean test -U -DpropertyName=propertyVal, where "${propertyName}" exists in the properties file).
What changes do I need to make in the pom.xml file to accomplish this?
UPDATE:
Adding the pom.xml so people can see what I am working with.
<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_Automation</groupId>
<artifactId>TA_ID</artifactId>
<version>1.0-SNAPSHOT</version>
<name>T_QE</name>
<properties>
<jdk.version>1.8</jdk.version>
<maven-surefire-version>2.19.1</maven-surefire-version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<src.dir>src/main/java</src.dir>
<test.dir>src/test/java</test.dir>
<test.resources>src/test/resources</test.resources>
</properties>
<build>
<testResources>
<testResource>
<directory>${test.resources}</directory>
<filtering>true</filtering>
</testResource>
</testResources>
<sourceDirectory>${src.dir}</sourceDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven-surefire-version}</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>target/test-classes/testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>default</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<propertyName>${propertyName}</propertyName>
</properties>
</profile>
</profiles>
UPDATE #2:
The property file (located at src/test/resources/configuration.properties) contents are below:
api.username=${propertyName}
There is a thing called filtering - https://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html.
Basically you add this to your build:
<build>
<testResources>
<testResource>
<directory>src/test/resources</directory>
<filtering>true</filtering>
</testResource>
</testResources>
</build>
Then inside your profile you would do:
<profile>
<id>myprofile</id>
...
<properties>
<propertyName>${propertyName}</propertyName>
</properties>
</profile>
Assuming in your .properties file you have:
myProperty = ${propertyName}
Try it out.
Update
This plugin has to be in your pom:
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
...
</plugins>
And you should add this to your maven execution command:
mvn resources:testResources

maven-compiler-plugin in parent pom

I am facing issue in setting java compiler version for the parent-child pom files. I added maven-compiler-plugin in child pom with version 1.7 and noticed that the java version changed from default 1.5 to 1.7 for child module and the parent project is still with 1.5. Then, I moved the compiler plugin from child pom to parent pom. Expected that the compiler version will be changed to 1.7 for both parent and child maven modules. But strangely no changes observed, the child module is still with 1.7 and the parent project is 1.5. I performed 'maven- > update project' from eclipse IDE. but no use. FYI : Building the parent-child projects is working fine with no issues. Any help? Here are my parent and child POMs
PARENT 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>com.mymaven.parentpom.example</groupId>
<artifactId>ParentPOMProj</artifactId>
<packaging>pom</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>ParentPOMProj</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>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>ParentPOMProj</finalName>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<compilerVersion>1.7</compilerVersion>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
<modules>
<module>ModuleOne</module>
</modules>
</project>
CHILD POM
<?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>com.mymaven.parentpom.example</groupId>
<artifactId>ParentPOMProj</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com.mymaven.parentpom.example.module</groupId>
<artifactId>ModuleOne</artifactId>
<version>0.0.2-SNAPSHOT</version>
<name>ModuleOne</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<finalName>ModuleOne</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
-----------------------------------------------------
In the parent you need to define it in <pluginManagement/> rather than <plugins/>
https://maven.apache.org/pom.html#Plugin_Management
pluginManagement: is an element that is seen along side plugins.
Plugin Management contains plugin elements in much the same way,
except that rather than configuring plugin information for this
particular project build, it is intended to configure project builds
that inherit from this one
<build>
<finalName>ParentPOMProj</finalName>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
As mentioned in the docs, the child project also needs to reference the plugin:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<!-- inherits config from parent: can override if required -->
</plugin>
</plugins>
</build>
After researching further, I understood that the java version in maven-compiler-plugin in parent pom applies to the child POMs but not to the parent itself. Basically, most of the time, it is not recommend to keep any source code in parent project, it is barely to handle all the build configuration for the child modules. Here are the updated POMS :
PARENT POM
http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
<groupId>com.mymaven.parentpom.example</groupId>
<artifactId>ParentPOMProj</artifactId>
<packaging>pom</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>ParentPOMProj</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>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>ParentPOMProj</finalName>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
<modules>
<module>ModuleOne</module>
</modules>
CHILD POM
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">
4.0.0
<parent>
<groupId>com.mymaven.parentpom.example</groupId>
<artifactId>ParentPOMProj</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com.mymaven.parentpom.example.module</groupId>
<artifactId>ModuleOne</artifactId>
<packaging>jar</packaging>
<version>0.0.2-SNAPSHOT</version>
<name>ModuleOne</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
You should check the compliance of the project. Right click on the project in the Project Explorer of Eclipse, then go to Properties, and set it as shown on the picture (you can also set it to version 1.7 if the proper JDK is installed):

Categories