View Javadoc

1   /*
2    * #%L
3    * Native ARchive plugin for Maven
4    * %%
5    * Copyright (C) 2002 - 2014 NAR Maven Plugin developers.
6    * %%
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   * 
11   * http://www.apache.org/licenses/LICENSE-2.0
12   * 
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   * #L%
19   */
20  package com.github.maven_nar;
21  
22  import java.io.File;
23  import java.util.List;
24  
25  import org.apache.maven.plugin.MojoExecutionException;
26  import org.apache.maven.plugin.MojoFailureException;
27  import org.apache.maven.plugins.annotations.Parameter;
28  
29  import com.github.maven_nar.cpptasks.CCTask;
30  import com.github.maven_nar.cpptasks.CUtil;
31  import com.github.maven_nar.cpptasks.types.CommandLineArgument;
32  import com.github.maven_nar.cpptasks.types.LibrarySet;
33  import com.github.maven_nar.cpptasks.types.LinkerArgument;
34  
35  /**
36   * Java specifications for NAR
37   *
38   * @author Mark Donszelmann
39   */
40  public class Java {
41  
42    /**
43     * Add Java includes to includepath
44     */
45    @Parameter(required = true)
46    private boolean include = false;
47  
48    /**
49     * Java Include Paths, relative to a derived ${java.home}. Defaults to:
50     * "${java.home}/include" and
51     * "${java.home}/include/<i>os-specific</i>".
52     */
53    @Parameter
54    private List includePaths;
55  
56    /**
57     * Add Java Runtime to linker
58     */
59    @Parameter(required = true)
60    private boolean link = false;
61  
62    /**
63     * Relative path from derived ${java.home} to the java runtime to link with
64     * Defaults to Architecture-OS-Linker
65     * specific value. FIXME table missing
66     */
67    @Parameter
68    private String runtimeDirectory;
69  
70    /**
71     * Name of the runtime
72     */
73    @Parameter(defaultValue = "jvm")
74    private String runtime = "jvm";
75  
76    private AbstractCompileMojo mojo;
77  
78    public Java() {
79    }
80  
81    public final void addIncludePaths(final CCTask task, final String outType)
82        throws MojoFailureException, MojoExecutionException {
83      if (this.include || this.mojo.getJavah().getJniDirectory().exists()) {
84        if (this.includePaths != null) {
85          for (final Object includePath : this.includePaths) {
86            final String path = (String) includePath;
87            task.createIncludePath().setPath(new File(this.mojo.getJavaHome(this.mojo.getAOL()), path).getPath());
88          }
89        } else {
90          final String prefix = this.mojo.getAOL().getKey() + ".java.";
91          final String includes = NarProperties.getInstance(this.mojo.getMavenProject()).getProperty(prefix + "include");
92          if (includes != null) {
93            final String[] path = includes.split(";");
94            for (final String element : path) {
95              task.createIncludePath().setPath(new File(this.mojo.getJavaHome(this.mojo.getAOL()), element).getPath());
96            }
97          }
98        }
99      }
100   }
101 
102   public final void addRuntime(final CCTask task, final File javaHome, final String os, final String prefix)
103       throws MojoFailureException {
104     if (this.link) {
105       if (os.equals(OS.MACOSX)) {
106         final CommandLineArgument.LocationEnum end = new CommandLineArgument.LocationEnum();
107         end.setValue("end");
108 
109         // add as argument rather than library to avoid argument quoting
110         final LinkerArgument framework = new LinkerArgument();
111         framework.setValue("-framework");
112         framework.setLocation(end);
113         task.addConfiguredLinkerArg(framework);
114 
115         final LinkerArgument javavm = new LinkerArgument();
116         javavm.setValue("JavaVM");
117         javavm.setLocation(end);
118         task.addConfiguredLinkerArg(javavm);
119       } else {
120         if (this.runtimeDirectory == null) {
121           this.runtimeDirectory = NarProperties.getInstance(this.mojo.getMavenProject()).getProperty(
122               prefix + "runtimeDirectory");
123           if (this.runtimeDirectory == null) {
124             throw new MojoFailureException("NAR: Please specify a <RuntimeDirectory> as part of <Java>");
125           }
126         }
127         this.mojo.getLog().debug("Using Java Runtime Directory: " + this.runtimeDirectory);
128 
129         final LibrarySet libset = new LibrarySet();
130         libset.setProject(this.mojo.getAntProject());
131         libset.setLibs(new CUtil.StringArrayBuilder(this.runtime));
132         libset.setDir(new File(javaHome, this.runtimeDirectory));
133         task.addLibset(libset);
134       }
135     }
136   }
137 
138   public final void setAbstractCompileMojo(final AbstractCompileMojo mojo) {
139     this.mojo = mojo;
140   }
141 }