Java compiles from command line but not from Maven - maven

I have my system set up to compile jee6 code from the command line (no IDE). I recently tried to compile some code that uses javax.enterprise.context.RequestedScope. The code compiled fine from the command prompt but when I tried to build the application using Maven I keep getting javax.enterprise.context package not found error.
What gives? I thought Maven was using the same javac I'm using. Why can javac find the package but Maven can't? Do I need to add dependencies for java packages?
Or...is there a way to tell Maven to use the current class files when building or does Maven have to compile when it builds?
Thanks.

Try adding the Java EE 6 jar to your Maven dependencies:
<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">
. . .
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>6.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
This will add the Java EE classes to your compile-time classpath.

To compile your code with Maven, you need to tell it where to find required components.
Looks like you need to add this to your dependencies:
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-endorsed-api</artifactId>
<version>6.0</version>
<scope>provided</scope>
</dependency>
Note the <scope> element. It tells Maven not to bundle this dependency with your artifact, because at run time it will be provided by the application container.
On the reason why it compiles from the command line.
You probably have the required jar file on the command line -cp option or in your global CLASSPATH variable.
Maven, by design, does not pay attention to the global CLASSPATH.

Related

Packaging multiple jars using maven

I am new to maven. I want to package my java code so that, it can be run only by running a command java jar.
My code uses 2 external jar files, 1 main java class, 1 property file and will output 1log file.
My POM :
<modelVersion>4.0.0</modelVersion>
<groupId>com.ibm.mq.jms</groupId>
<artifactId>com.ibm.mq</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.ibm.mq</groupId>
<artifactId>com.ibm.mq.allclient</artifactId>
<version>9.0.4.0</version>
</dependency>
<dependency>
<groupId>javax.jms</groupId>
<artifactId>javax.jms-api</artifactId>
<version>2.0.1</version>
</dependency>
</dependencies>
How can i create a single jar file that could be run independently?
Is it better to have this complied in a zip file?
My property file needs to be changed by user. Where should I put my property file?
Generally, try one of the solutions in How can I create an executable JAR with dependencies using Maven?
The property file must be outside the final jar so that the user can edit them.

how to add my one project jar in another maven project in netbeans

I have one project jar oauth.
I want to add it in another maven project . I tried to change pom.xml file but no effect. Can anyone please suggest me?
I tried to add following dependency in my pom.xml file:
<dependency>
<groupId>com.payupaisa.oauth</groupId>
<artifactId>auth</artifactId>
<version>1.0.0</version>
<type>jar</type>
<scope>system</scope>
<systemPath>${basedir}/src/main/webapps/WEB-INF/lib/auth.jar</systemPath>
</dependency>
With the assumption that you have that auth.jar in your local repository (as it builds fine).
Why don't you give a try like this.
<dependency>
<groupId>com.payupaisa.oauth</groupId>
<artifactId>auth</artifactId>
<version>1.0.0</version>
<scope>compile</scope>
</dependency>
Honestly speaking I don't prefer to give the jar location in my pom file and using scope as system, I leave this task to handle by Maven to resolve all the artifacts either by searching in local maven repository first(/.m2) or in MAVEN CENTRAL REPO if it is a 3rd party jar.

Maven: Including external JAR in POM

I know questions like this have been asked and answered before, and have taken the time to read those threads, but somehow they won't help me. I have locally added own Java code to the DSpace software, and my code depends on another library, so I'll have to include this library (JAR) into the <dependencies> section of the POM, right? This is what my entry looks like:
<dependency>
<groupId>de.mannheim.ids</groupId>
<artifactId>pid</artifactId>
<version>0.0.1-SNAPSHOT</version>
<systemPath>/absolute/path/to/jar/pid-client-0.0.1-SNAPSHOT.jar</systemPath>
<scope>system</scope>
</dependency>
This does not help but results in a Compile Error - the relevant classes cannot be found. I have also tried mvn install:install-file -Dfile..., which tells me everything is fine with the jar (BUILD SUCCESS), but the subsequent mvn package fails with the usual error.
What could I be doing wrong?
You should be able to use the artifact as usual after using mvn install:install.
If that command returned Build successful you should be able to use this in your pom:
<dependency>
<groupId>de.mannheim.ids</groupId>
<artifactId>pid</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
Maven will find it like it would find any jar in your local repository.

Maven can't find httpcomponents-client in repository

I am trying to add the Apache httpcomponents-client library to my Maven project. I have added a dependency to pom.xml (as found on http://mvnrepository.com/artifact/org.apache.httpcomponents/httpcomponents-client/4.1.1), but when building my Eclipse project Maven is unable to find and download the library.
I have made a test project which does nothing but include this library to ensure that it's not any other settings that cause problems:
<?xml version="1.0"?>
<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>no.gundelsby.test</groupId>
<artifactId>NeedMyPackage</artifactId>
<version>0.1</version>
<dependencies>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcomponents-client</artifactId>
<version>4.1.1</version>
</dependency>
</dependencies>
</project>
Building this project results in the same error.
Other things I have tested:
Tried building with vanilla installs of both Maven 2.2.1 and 3.0.3
Had a friend build the test project on his computer to rule out local problems on my computer
Changed the version from 4.1.1 to 4.1
For what it's worth I had the same problem a few days ago with org.easytesting.fest-swing, see pom dependency entry below:
<dependency>
<groupId>org.easytesting</groupId>
<artifactId>fest-swing</artifactId>
<version>1.2.1</version>
</dependency>
You don't want httpcomponents-client as a dependency. That's just the parent pom for the client-related modules. I suspect you actually want <artifactId>httpclient</artifactId>.
Here a simple solution...
Its great for me...
You can downloar jar file from internet and add manually to project from netbeans or another IDE.
Here an example for you:
enter image description here
Search the downloaded jar in your computer
enter image description here

How to generate Javadoc for Maven Dependencies

I have a maven project with the following POM snippet:
<modelVersion>4.0.0</modelVersion>
<artifactId>Foo-Deploy</artifactId>
<name>Foo-Deploy</name>
<packaging>pom</packaging>
<description>foobar</description>
<dependencies>
<dependency>
<groupId>de.foo.bar</groupId>
<artifactId>some-api</artifactId>
<version>${project.version}</version>
<classifier>doc</classifier>
<type>zip</type>
</dependency>
</dependencies>
The idea is to have a dependency defined in which some sources are (this is created successfully before).
Now I want to run javadoc on exactly THIS dependency. When I call
mvn javadoc:jar -DincludeDependencySources=true -DdependencySourceIncludes=de.foo.bar:some-api:*:doc:zip
it fails with the message
Not executing Javadoc as the project
is not a Java classpath-capable
package
what is wrong ? and would it work anyhow ?
or how can I generate javadoc from a specific dependency (assuming this project has more dependencies) ?
Thanks
To generate javadoc for dependent sources, a sequence of steps needs to be done. These are outlined in this link.
Essentially you need to ensure that the source files of the dependency is generated/available and <includeDependencySources> parameter is enabled.

Resources