Pages

Using MAVEN behind HTTP proxy

Most organization do not allow the employee to connect their devices directly to internet. They use HTTP proxy server to access the internet. All the traffic goes through this HTTP server. To use MAVEN behind HTTP proxy we have to define some setting that contains the address of proxy server.

Using maven behind proxy

To use maven behind the proxy we have to define the proxy setting in setting.xml  file, inside the proxy element.

The setting file is found in /etc/maven/setting.xml

Add the following line under proxy element


<settings>
    <proxies>
          <proxy>
              <active>true</active>
              <protocol>http</protocol>
              <host>192.168.1.100</host>
             <port>3128</port>
             <username>your-username</username>
            <password>your-password</password>
       </proxy>
  </proxies>
</settings>
What is Maven ?



Know the use of Scope in Maven Dependency

Maven dependency scope attribute is used to specify the visibility of a dependency, relative to the different lifecycle phases (build, test, runtime etc). Maven provides six scopes i.e.
  1. compile 
  2. provided 
  3. runtime
  4. test
  5. system
  6. import

scope – compile

This is maven default scope. Dependencies with compile scope are needed to build, test, and run the project.

Scope compile is to be required in most of the cases to resolve the import statements into your java classes source code.

<dependency>
 <groupId>joda-time</groupId>
 <artifactId>joda-time</artifactId>
 <version>2.7</version>
 <scope>compile</scope>
</dependency>

scope – provided

The scope provided is used during build and test the project. They are also required to run, but should not exported, because the dependency will be provided by the runtime, for instance, by servlet container or application server.

<dependency>
 <groupId>javax.servlet.jspt</groupId>
 <artifactId>jsp-api</artifactId>
 <version>2.1</version>
 <scope>provided</scope>
</dependency>

scope – runtime

The scope runtime are not needed to build, but are part of the classpath to test and run the project, dependencies marked with the runtime scope will be present in runtime and test classpath, but they will be missing from compile classpath.

A good example of dependencies that should use the runtime scope is a JDBC driver:

<dependency>
 <groupId>mysql</groupId>
 <artifactId>mysql-connector-java</artifactId>
 <version>6.0.6</version>
 <scope>runtime</scope>
</dependency>


scope – test
The scope test are not needed to build and run the project. They are needed to compile and run the unit tests.

<dependency>
 <groupId>junit</groupId>
 <artifactId>junit</artifactId>
 <version>4.11</version>
 <scope>test</scope>
</dependency>

scope – system

Dependencies with system are similar to ones with scope provided. The only difference is system dependencies are not retrieved from remote repository or Central repository. They are present under project’s subdirectory and are referred from there.

<dependency>
 <groupId>xyz</groupId>
 <artifactId>xyz</artifactId>
 <version>2.11</version>
 <scope>system</scope> 
        <systemPath> ${basedir}/src/lib/xyz.jar </systemPath> 
</dependency>

scope – import

import scope is only supported on a dependency of type pom in the dependencyManagement section. It indicates the dependency to be replaced with the effective list of dependencies in the specified POM’s dependencyManagement section.


<dependency>
 <groupId>other.pom.group.id</groupId>
 <artifactId>other-pom-artifact-id</artifactId>
 <version>4.11</version>
 <scope>import</scope> 
        <type>POM</type>
 </dependency>

Maven dependency transitivity resolution
When you include a maven dependency and it has it’s own other dependencies (i.e. transitive dependencies) then you may want to be clear about the scope of these transitive dependencies as well.

    For the compile scope, all dependencies with runtime scope will be pulled in with the runtime scope, in the project and all dependencies with the compile scope will be pulled in with the compile scope, in the project
    For the provided scope, both runtime and compile scope dependencies will be pulled in with the provided scope, in the project
    For the test scope, both runtime and compile scope transitive dependencies will be pulled in with the test scope, in the project
    For the runtime scope, both runtime and compile scope transitive dependencies will be pulled in with the runtime scope, in the project