Dependency not accept - java

I have this problem : My dependency org.apache.poi can not accept in pom.xml
this dependency lighting red. But i add in project module jars:
poi-5.2.2
poi-ooxml-5.2.2
commons-collections 4-4.3
commons-compress-1.18
xmlbeans-3.1.0
poi-ooxml-schemas-3.9
dom4j-1.6.1
all jars i am add but dependency not accepted and continue lighting red: my code
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>5.2.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.2.2</version>
</dependency>

Those Maven coordinates are correct.
Here is an example POM based on the artifact maven-archetype-quickstart, with updated versions on all items, and with your your two dependencies pasted as-is. And I updated the import statements in the AppTest.java file to use JUnit Jupiter. So we have fully-working, up-to-date, practical example app.
Your dependencies are processed correctly by Maven. This app compiles and runs. I did this in IntelliJ 2022.1.
<?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>work.basil.example</groupId>
<artifactId>TryDep</artifactId>
<version>1.0-SNAPSHOT</version>
<name>TryDep</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.8.2</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>5.2.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.2.2</version>
</dependency>
</dependencies>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.2.0</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.9.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>3.0.0-M1</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>3.0.0-M1</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.9.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.2.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
As a sanity check, I suggest you create and run a new project using that POM posted above.
In your own problematic project, I suggest you make sure Maven processed your POM. In IntelliJ, either:
Click the little floating windoid with a Maven logo.
Click the two arrows in a circle icon in the Maven panel, a button whose tooltip says Reload All Maven Projects.
Then execute a Maven clean and install.
Your locale Maven cache in a .m2 folder may need to download the dependencies which may take several minutes depending on the speed of your Internet access.
On occasion, the Maven local cache goes wonky. If all else fails, delete the entire .m2 folder. Then do another clean and install which in turn should trigger creation and population of a fresh .m2 folder.

Related

Maven pom file, what is it doing?

I have just started using Maven, in a newbie capacity, just want to understand something around dependencies.
I am trying to build a micro web service using iText and the pdf output functionality.
So my very first steps is seeing if I can get a pdf output from a very simple Java program.
In my pom file i have the following dependencies:
<!-- iText Core -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext7-core</artifactId>
<version>${itext.version}</version>
<type>pom</type>
</dependency>
<!-- iText pdfHTML add-on -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>html2pdf</artifactId>
<version>2.1.6</version>
</dependency>
After reading the information on the Maven site, the pom file should do all of the heavy lifting in getting the dependencies, this is the bit i'm a little confused on.
Will the pom file physically download the files to the the app location on application start so that he app can utilize these files?
if that's the case it doesn't seem to be doing this and so am I missing something in the pom file to enable this?
The full pom file is:
<?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.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1.0-SNAPSHOT</version>
<name>my-app</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<itext.version>RELEASE</itext.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- iText Core -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext7-core</artifactId>
<version>${itext.version}</version>
<type>pom</type>
</dependency>
<!-- iText pdfHTML add-on -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>html2pdf</artifactId>
<version>2.1.6</version>
</dependency>
</dependencies>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
Any help appreciated.
Thanks
When maven build is executed, Maven automatically downloads all the dependency jars into the local repository.
the local repository of Maven is a folder location on the developer's machine, where all the project artifacts are stored locally.
Usually this folder is named .m2.
Here's where the default path to this folder is – based on OS:
Windows: C:\Users\User_Name\ .m2
Linux: /home/User_Name/.m2
Mac: /Users/user_name/.m2
https://www.baeldung.com/maven-local-repository
Maven does download the dependencies to the local m2 repository. But this is more meant for building the application, not for running.
What you want (copy the dependencies next to the output jar) can be achieved with the goal dependency:copy-dependencies
See this blog post:
https://technology.amis.nl/2017/02/09/download-all-directly-and-indirectly-required-jar-files-using-maven-install-dependencycopy-dependencies/
Managing dependencies is one of the key features of Maven.
Dependency management: It is possible to define dependencies to other
projects. During the build, the Maven build system resolves the
dependencies and it also builds the dependent projects if needed.
Resolving dependencies does mean it downloads all the specified jars in the local system.
The Maven tooling reads the pom file and resolves the dependencies of
the project. Maven validates if required components are available in a
local repository. The local repository is found in the .m2/repository
folder of the users home directory.
Note that .m2/ is a hidden folder. If you are using Linux, would be this path /home/someuser/.m2
Read this
If however its not downloading the jars or creating the .m2 directory at all, then either you are not building the project right or you are not connected to the internet.

How can I generate a maven repository-ready library with swagger-codegen-maven-plugin?

I'm using the Maven plugin swagger-codegen-maven-plugin to generate a Java client jar. I put my swagger.json in the src/main/resources folder and ran mvn clean install. Here 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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>my-java-client</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>my-java-client</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<swagger.version>1.5.13</swagger.version>
</properties>
<dependencies>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp</groupId>
<artifactId>okhttp</artifactId>
<version>2.7.5</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp</groupId>
<artifactId>logging-interceptor</artifactId>
<version>2.7.5</version>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-codegen</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>com.squareup.okio</groupId>
<artifactId>okio</artifactId>
<version>1.12.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>io.swagger</groupId>
<artifactId>swagger-codegen-maven-plugin</artifactId>
<version>2.2.2</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>src/main/resources/swagger.json</inputSpec>
<modelPackage>mypackage.model</modelPackage>
<apiPackage>mypackage</apiPackage>
<invokerPackage>mypackage.invoker</invokerPackage>
<language>java</language>
<configOptions>
<interfaceOnly>true</interfaceOnly>
<dateLibrary>java8</dateLibrary>
<java8>true</java8>
</configOptions>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
When I run mvn clean install, a jar file my-java-client-1.0-SNAPSHOT.jar is made in the target folder. It has source code in it, but no pom.xml file. There is a pom.xml file in the target/generated-sources/swagger folder but it has the groupId and artifactId:
<groupId>io.swagger</groupId>
<artifactId>swagger-java-client</artifactId>
The README.md file in the target/generated-sources/swaggersays to include the following in your pom.xml to use the generated jar:
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-java-client</artifactId>
<version>1.0.0</version>
<scope>compile</scope>
</dependency>
I'm guessing these are just defaults because if I generated multiple swagger clients they would conflict over the name, and I can see in the swagger-codegen source code that these fields are generated from placeholders like {{artifactId}}. I haven't been able to find where I can set these placeholders.
How can I get the jar to include a pom.xml with an artifactId and groupId of my choosing, so I can upload it to a Maven repository like Artifactory, and use it in my Maven dependencies?
It's not particularly well documented, but it appears you are able to override the artifactId, groupId etc from within the configOptions:
<configOptions>
<groupId>com.your.group.id</groupId>
<artifactId>your-artifact-id</artifactId>
<artifactVersion>0.0.1-SNAPSHOT</artifactVersion>
</configOptions>
Many of the properties inside CodegenConstants.java can be set in the same way.
I updated it using below config Properties (gradle)
'groupId' : 'your.group.id',
'artifactId' : 'your-artifact-id'
you can also go through document which is helpful for reference of the properties.
https://openapi-generator.tech/docs/generators/kotlin/

PMD rule set is not getting downloaded/updated through MAVEN

I tried to integrate PMD in one of my project (I am using MAVEN Build tool)
When I try to integrate, I can see XML configuration files are mandatory.
I have tried to download PMD plugin - I expected global ruleset files might be available in PMD plug in, but they are not.
I used below link:
https://sourceforge.net/projects/pmd/?source=typ_redirect
After googling, I have seen one link to get ruleset
http://grepcode.com/file/repo1.maven.org/maven2/pmd/pmd/4.3
I cant download all XML files.
Is there any way to download/update through build or can we get all XML files in one location anywhere? I tried my level best to search in google and couldn't figure it out.
I attached pom.xml here. Can you please let me know how to add my ruleset automatically whenever PMD updated automatically?
<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.scm</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>SCM-PRODUCT</name>
<description>SCM Product for learning purpose</description>
<properties>
<java.version>1.7</java.version>
<hibernate.validator.version>5.2.4.Final</hibernate.validator.version>
<javax.el-api.version>2.2.4</javax.el-api.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<checkstyle-config-url>
D:/rules/checkstyle/2.0/checkstyle-2.0.xml
</checkstyle-config-url>
<checkstyle.version>6.18</checkstyle.version>
<log4j.version>1.2.17</log4j.version>
<!-- TEST CASES RELATED BEGINS-->
<junit.version>4.12</junit.version>
<!-- TEST CASES RELATED ENDS HERE-->
<!-- STATIC CODE ANALYSIS PROPERTIES -->
<findbugs.plugin.version>3.0.3</findbugs.plugin.version> <!-- Reports on common code mistakes and pitfalls -->
<checkstyle.plugin.version>5.0</checkstyle.plugin.version> <!-- Checks Code Style for Developers -->
<pmd.plugin.version>3.6</pmd.plugin.version> <!-- Source Code Analyzer -->
<doxia.module.markdown.version>1.3</doxia.module.markdown.version>
<javadoc.plugin>2.8.1</javadoc.plugin> <!-- Generates JavaDoc -->
<jxr.plugin>2.3</jxr.plugin> <!-- Cross reference report of project source code -->
<!-- REPORTING TOOL PROPERTIES -->
<project.info.reports.plugin>2.4</project.info.reports.plugin> <!-- A plethora of miscellaneous report: info, ci, dependencies, scm, plugins, etc. -->
<site.plugin>3.1</site.plugin>
<sonar.plugin>3.2-RC3</sonar.plugin> <!-- Analysis and metrics on code over time -->
<surefire.plugin>2.12</surefire.plugin> <!-- Reports Test Results -->
<taglist.plugin>2.4</taglist.plugin> <!-- Reports on Tags such as #todo and //TODO -->
<versions.plugin>1.3.1</versions.plugin>
<maven-compiler-plugin>3.1</maven-compiler-plugin>
<cobertura.plugin>2.5.1</cobertura.plugin> <!-- Reports Test Coverage -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<modules>
<module>services</module>
<module>presentation</module>
<module>service_validator</module>
<module>jsonvo</module>
</modules>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>${hibernate.validator.version}</version>
</dependency>
<dependency>
<groupId>javax.el</groupId>
<artifactId>javax.el-api</artifactId>
<version>${javax.el-api.version}</version>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.1.0.Final</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<!-- http://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<!-- http://mvnrepository.com/artifact/net.sourceforge.pmd/pmd -->
<dependency>
<groupId>net.sourceforge.pmd</groupId>
<artifactId>pmd</artifactId>
<version>5.4.2</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugin</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin}</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>2.17</version>
<configuration>
<includeTests>true</includeTests>
<rulesets>
<ruleset>${checkstyle-config-url}</ruleset>
</rulesets>
<minimumTokens>100</minimumTokens>
<targetJdk>${java.version}</targetJdk>
<failOnViolation>true</failOnViolation>
</configuration>
<executions>
<execution>
<phase>test</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<version>${pmd.plugin.version}</version>
<configuration>
<targetJdk>${java.version}</targetJdk>
<minimumTokens>20</minimumTokens>
<skipEmptyReport>false</skipEmptyReport>
<failOnViolation>true</failOnViolation>
<printFailingErrors>true</printFailingErrors>
<!--<includeTests>true</includeTests>-->
<rulesets>
<ruleset>${pom.basedir}/pmd-rulesets.xml</ruleset>
</rulesets>
<!--
<excludeRoots>
<excludeRoot>target/generated-sources/antlr</excludeRoot>
<excludeRoot>target/generated-sources/antlr/com/puppycrawl/tools/checkstyle/grammars/javadoc</excludeRoot>
</excludeRoots>
-->
</configuration>
<executions>
<execution>
<goals>
<goal>pmd</goal>
<goal>cpd</goal>
<goal>cpd-check</goal>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>findbugs-maven-plugin</artifactId>
<version>${findbugs.plugin.version}</version>
<configuration>
<effort>Max</effort>
<threshold>Low</threshold>
<excludeFilterFile>config/findbugs-exclude.xml</excludeFilterFile>
</configuration>
</plugin>
</plugins>
</pluginManagement>
<plugins>
</plugins>
</build>
</project>
If you don't need to use your custom pmd rulesets you can omit the rulesets tag altogether.
If you want to use only some of pmd rulesets you can use predefined ones:
<rulesets>
<ruleset>/rulesets/java/braces.xml</ruleset>
<ruleset>/rulesets/java/naming.xml</ruleset>
</rulesets>
You are using version 3.6 of the maven-pmd-plugin. There is a default value for the rulesets - it's java-basic, java-imports and java-unusedcode. See the maven-pmd-plugin documentation.
If you want to start with these rulesets, you can omit the rulesets tag altogether, as krzyk mentioned.
Maven Plugin 3.6 uses PMD 5.3.5 - so downloading rulesets for PMD 4.3 will not work.
But, you don't need to download the rulesets. You can create your own custom ruleset, which references the rules you want to have checked in your code. And this would be your file pmd-rulesets.xml.
Is there any way to download/update through build or can we get all XML
files in one location anywhere?
There is no such a ruleset. Enabling all rules PMD provides, doesn't make sense, as some rules contradict each other. Please read "Best Practices": Choose the rules that are right for you.
Can you please let me know how to add my ruleset automatically whenever PMD
updated automatically?
You don't need to add your ruleset - you are using it already. However, if a new PMD version has new rules, you won't necessarily have these new rules activated. So, you might want to read the release notes of PMD and checkout if there are new interesting rules. Then you can reference the new rules in your ruleset file.
For the java language, you can see the available rules in the Rulesets index.

Maven 3 - How to add annotation processor dependency?

I need to run an annotation processor on my project's sources. The annotation processor should not become a transitive dependency of the project since it's only needed for annotation processing and nothing else.
Here is the complete (non-working) test pom I use for this:
<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>test</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>Test annotations</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<hibernate-jpamodelgen.version>1.2.0.Final</hibernate-jpamodelgen.version>
</properties>
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>6.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<annotationProcessors>
<annotationProcessor>
org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor</annotationProcessor>
</annotationProcessors>
<debug>true</debug>
<optimize>true</optimize>
<source>1.6</source>
<target>1.6</target>
<compilerArguments>
<AaddGeneratedAnnotation>true</AaddGeneratedAnnotation>
<Adebug>true</Adebug>
</compilerArguments>
</configuration>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-jpamodelgen</artifactId>
<version>${hibernate-jpamodelgen.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
I explicitly defined org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor as an annotation processor in the plugin configuration for tests and I know it shouldn't be required.
The problem I'm encountering is that the hibernate-jpamodelgen dependency is not added to the compiler classpath so the annotation processor is not found and the build fails.
As per this answer, I tried adding the dependency as a build extension (not sure I understand what those are supposed to be!) like so:
<extensions>
<extension>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-jpamodelgen</artifactId>
<version>${hibernate-jpamodelgen.version}</version>
</extension>
</extensions>
This also doesn't add hibernate-jpamodelgen to the compiler classpath.
The only thing I found which works so far is adding the dependency to the project in the <dependencies> section. This has the unfortunate side-effect of adding hibernate-jpamodelgen as a transitive dependency afterwards which I want to avoid.
My previous working setup uses the maven-processor-plugin plugin to achieve what I want. However, this plugin is not supported by eclipse m2e and the latest version of the maven-compiler-plugin now handles multiple compiler arguments properly so I'd rather use the latter.
The annotationProcessorPaths option can be used in recent versions of the Maven compiler plug-in:
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<annotationProcessorPaths>
<annotationProcessorPath>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-jpamodelgen</artifactId>
<version>5.2.6.Final</version>
</annotationProcessorPath>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</pluginManagement>
That way the processor is separated from the actual project dependencies. This option is also picked up by the Eclipse M2E plug-in if annotation processing is enabled for the project.
Add the dependency as an optional dependency (<optional>true</optional>). This will add the dependency under compilation, but will prevent it for being a transitive dependency:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-jpamodelgen</artifactId>
<version>${hibernate-jpamodelgen.version}</version>
<optional>true</optional>
</dependency>
If you're creating an artifact in this module with all your dependencies in it (like a .war), you may use the <scope>provided</scope> instead. This both prevents the dependency to be transitive and to be included in the artifact the module produces.
For JDK 10 I really had to go a bit crazy to get it to work, Hoping someone finds this useful
<jaxb.version>2.3.0</jaxb.version>
<maven.hibernate.version>5.3.2.Final</maven.hibernate.version>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven.compiler.version}</version>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/generated-sources/annotations</outputDirectory>
<annotationProcessorPaths>
<annotationProcessorPath>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-jpamodelgen</artifactId>
<version>${maven.hibernate.version}</version>
</annotationProcessorPath>
<annotationProcessorPath>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>${jaxb.version}</version>
</annotationProcessorPath>
</annotationProcessorPaths>
<annotationProcessors>
<annotationProcessor>org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor</annotationProcessor>
</annotationProcessors>
<compilerArgs>
<arg>-AaddGeneratedAnnotation=false</arg>
</compilerArgs>
<compilerArguments>
<AaddGeneratedAnnotation>false</AaddGeneratedAnnotation>
<Adebug>true</Adebug>
</compilerArguments>
<failOnError>true</failOnError>
</configuration>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-jpamodelgen</artifactId>
<version>${maven.hibernate.version}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>${jaxb.version}</version>
<type>jar</type>
</dependency>
</dependencies>
</plugin>
The problem is really in 3.* version of the maven-compiler-plugin. It acts a bit different from the 2.* version. In particular, it seems that maven-compiler-plugin3.* doesn't add its dependencies and build extensions to the classpath because it uses javax.tools instruments for running compile process. To get back the old behavior for maven-compiler-plugin you should use a new configuration property forceJavacCompilerUse:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<forceJavacCompilerUse>true</forceJavacCompilerUse>
</configuration>
....
</plugin>
Please take a look jpa-metamodels-with-maven
For further visitors, I found that there are some significant changes in maven-compiler-plugin 3.x series.
This is how I do this. (I'm the one who you linked)
The point is that my solution does not work with those 3.x series of maven-compiler-plugin.
<project ...>
<build>
<extensions>
<extension>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-jpamodelgen</artifactId>
<version>1.3.0.Final</version>
</extension>
</extensions>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version> <!-- See this? -->
</plugin>
</plugins>
</build>
</project>
Not sure what kind of build error you got, but here is my case:
I got the following compile error in Idea:
Annotation processor 'org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor' not found error
But, when compiled from Maven, it was all fine.
So, the problem of mine was that somehow I got wrong configuration in Idea settings. Particularly, it appeared that Idea somehow detected the processor and put in into the settings of module processor profiles. It is discussed here.
I fixed it as the following:
Go to Idea > Settings > Annotation Processors.
For each processor profile make sure that:
Enable annotation processing is Yes;
There is no annotation processor FQ name of one you have error about (e.i. "JPAMetaModelEntityProcessor") in the list on the right side. If it is listed there, just select and click '-' minus button to remove it.
I think this is a better way to contain such dependencies in profiles to solve such problems.
<profile>
<id>XXX-profile</id>
<dependencies>
<dependency>
// XXX artifact path
</dependency>
</dependencies>
</profile>

maven support for android projects?

I want to start a project for Android but i really like to build it using Maven. Does Google provide support for Maven or plan to support it? It would be great if anybody know at least an archetype for Maven that I can use meanwhile. Thanks in advance.
Seems like there is a maven plugin for that :-)
What I really wanted was an article like this, but i found it after the question was answered.
Update:
People at SpringSource did a Spring Android project that supports the usage of the Spring Framework in an Android environment and have Maven support. I will give it a try.
Here is an article about Spring Android and Maven using Eclipse 3.6 and Android SDK 9, split in two parts:
First part
Second part
The Android Maven plugin has been updated to support changes made in the Android SDK r14 release. You will need to adjust your POM to use the new version. I experienced an out of memory error with the new version when building my app, so note the dex jvmArguments section to allow for more available memory.
<plugin>
<groupId>com.jayway.maven.plugins.android.generation2</groupId>
<artifactId>android-maven-plugin</artifactId>
<version>3.0.0-alpha-13</version>
<configuration>
<sdk>
<platform>${android-platform}</platform>
</sdk>
<dex>
<jvmArguments>
<jvmArgument>-Xms256m</jvmArgument>
<jvmArgument>-Xmx512m</jvmArgument>
</jvmArguments>
</dex>
<deleteConflictingFiles>true</deleteConflictingFiles>
<undeployBeforeDeploy>true</undeployBeforeDeploy>
</configuration>
<extensions>true</extensions>
</plugin>
The latest version of the Android Configurator (m2e-android) for Eclipse also supports the changes in r14. Lastly, I've posted a follow up blog on the SpringSource site called Updated Maven Support for Android Projects, which goes over these updates to the tools.
Here is what I did to add maven support to an existing android project in Eclipse. I installed the 'm2e' plugin via the Eclipse market place. Then I installed the 'm2e-android' plugin. At the moment it is called 'Android Configurator for M2E' in Eclipse market place. After installing the two plugins and restarting Eclipse, right click on an existing android project-->Configure-->Convert to Maven Project. Choose a unique group id and an artifact id for your project then click finish. Copy the following contents to the pom.xml of the project and replace all the existing contents. Change the value of the 'version' tag under 'dependencies' to the SDK version you are using. Also change the value of the 'platform' tag near the end of the file to the value of your platform. Do not forget to also change the groupId, artifactId and name of the 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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.myproject</groupId>
<artifactId>MyProject</artifactId>
<version>0.1.0-SNAPSHOT</version>
<packaging>apk</packaging>
<name>MyProject</name>
<dependencies>
<dependency>
<groupId>com.google.android</groupId>
<artifactId>android</artifactId>
<version>2.3.3</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
<sourceDirectory>src</sourceDirectory>
<pluginManagement>
<plugins>
<plugin>
<groupId>com.jayway.maven.plugins.android.generation2</groupId>
<artifactId>android-maven-plugin</artifactId>
<version>3.3.0</version>
<extensions>true</extensions>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>com.jayway.maven.plugins.android.generation2</groupId>
<artifactId>android-maven-plugin</artifactId>
<configuration>
<sdk>
<!-- platform or api level (api level 4 = platform 1.6)-->
<platform>10</platform>
</sdk>
</configuration>
</plugin>
</plugins>
</build>
</project>
After that right click on the project-->Maven-->Update Project. If Eclipse is complaining about errors in the xml, you may want to install maven then run
mvn clean install
from the console in the folder that contains the pom.xml file.
as #Riduidel said before, you can use com.jayway.maven.plugins.android.generation2 plugin. Note, that you don't need download any plugins, you need to have just the maven for using this plugin.
How I did it:
manually add pom.xml to your android project (to the root of project).
download apache-maven-3.1.1 and add your bin folder ( ex D:\java\apache-maven-3.1.1\bin;) to path in Environment Variables.
configure settings.xml in [Your_maven_path]\conf with next:
<pluginGroups>
<pluginGroup>com.jayway.maven.plugins.android.generation2</pluginGroup>
</pluginGroups>
Add content to pom.xml. My example:
<?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>com.example.ENumbers</groupId>
<artifactId>ENumbers</artifactId>
<version>1.0</version>
<packaging>apk</packaging>
<name>MainApp</name>
<properties>
<platform.version>2.2.1</platform.version>
</properties>
<dependencies>
<dependency>
<groupId>com.google.android</groupId>
<artifactId>android</artifactId>
<version>${platform.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.simpleframework</groupId>
<artifactId>simple-xml</artifactId>
<version>2.7.1</version>
<scope>provided</scope>
<exclusions>
<exclusion>
<artifactId>xpp3</artifactId>
<groupId>xpp3</groupId>
</exclusion>
<exclusion>
<artifactId>stax-api</artifactId>
<groupId>stax</groupId>
</exclusion>
<exclusion>
<artifactId>stax</artifactId>
<groupId>stax</groupId>
</exclusion>
<exclusion>
<artifactId>spring-web</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
<exclusion>
<artifactId>commons-logging</artifactId>
<groupId>commons-logging</groupId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<groupId>com.jayway.maven.plugins.android.generation2</groupId>
<artifactId>android-maven-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<androidManifestFile>${project.basedir}/AndroidManifest.xml</androidManifestFile>
<assetsDirectory>${project.basedir}/assets</assetsDirectory>
<resourceDirectory>${project.basedir}/res</resourceDirectory>
<nativeLibrariesDirectory>${project.basedir}/src/main/native</nativeLibrariesDirectory>
<sdk>
<path>
D:\Program Files\Android\android-sdk
</path>
<platform>22</platform>
</sdk>
<undeployBeforeDeploy>true</undeployBeforeDeploy>
</configuration>
<extensions>true</extensions>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
Look at important <packaging>, <build> nodes, it's content and com.google.android dependency.
Now you can open Maven window in your IDE. For Intellij Idea I do it next:
Edit->Tool Windows->Maven and add your pom.xml for initializing maven directory.
That's all.

Categories

Resources