简体   繁体   English

如何使用AspectJ创建可运行的jar文件?

[英]How to create a runnable jar file with AspectJ?

I'm using Eclipse to compile java code with aspectj. 我正在使用Eclipse用Aspectj编译Java代码。 My code (with the aspect) works fine, but I'm struglling to create a jar file of this my code. 我的代码(具有方面)可以正常工作,但是我正在努力为此代码创建一个jar文件。

For example, if I select Export > Runnable JAR file, in the combo box I don't see my project (all the other project that not based on aspecj are showing up in the combo box). 例如,如果选择“导出”>“可运行的JAR文件”,则在组合框中看不到我的项目(所有其他基于aspecj的其他项目都显示在组合框中)。

If you like to have repeatable builds which work from command line as well as from IDEs like Eclipse or IntelliJ IDEA, I recommend a Maven build. 如果您希望具有可重复的版本,这些版本可以在命令行以及Eclipse或IntelliJ IDEA之类的IDE中运行,则建议使用Maven版本。 The onejar-maven-plugin creates a runnable JAR including AspectJ runtime and all other declared dependencies for you: onejar-maven-plugin为您创建一个可运行的JAR,包括AspectJ运行时和所有其他声明的依赖项:

<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>de.scrum_master.stackoverflow</groupId>
    <artifactId>my_project</artifactId>
    <version>1.0.0-SNAPSHOT</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.source-target.version>1.7</java.source-target.version>
        <aspectj.version>1.8.8</aspectj.version>
        <main-class>de.scrum_master.app.Application</main-class>
    </properties>

    <build>

        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.3</version>
                    <configuration>
                        <source>${java.source-target.version}</source>
                        <target>${java.source-target.version}</target>
                        <!-- IMPORTANT -->
                        <useIncrementalCompilation>false</useIncrementalCompilation>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>aspectj-maven-plugin</artifactId>
                    <version>1.8</version>
                    <configuration>
                        <!--<showWeaveInfo>true</showWeaveInfo>-->
                        <source>${java.source-target.version}</source>
                        <target>${java.source-target.version}</target>
                        <Xlint>ignore</Xlint>
                        <complianceLevel>${java.source-target.version}</complianceLevel>
                        <encoding>${project.build.sourceEncoding}</encoding>
                        <!--<verbose>true</verbose>-->
                        <!--<warn>constructorName,packageDefaultMethod,deprecation,maskedCatchBlocks,unusedLocals,unusedArguments,unusedImport</warn>-->
                    </configuration>
                    <executions>
                        <execution>
                            <!-- IMPORTANT -->
                            <phase>process-sources</phase>
                            <goals>
                                <goal>compile</goal>
                                <goal>test-compile</goal>
                            </goals>
                        </execution>
                    </executions>
                    <dependencies>
                        <dependency>
                            <groupId>org.aspectj</groupId>
                            <artifactId>aspectjtools</artifactId>
                            <version>${aspectj.version}</version>
                        </dependency>
                    </dependencies>
                </plugin>
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>exec-maven-plugin</artifactId>
                    <version>1.4.0</version>
                    <configuration>
                        <mainClass>${main-class}</mainClass>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.dstovall</groupId>
                    <artifactId>onejar-maven-plugin</artifactId>
                    <version>1.4.4</version>
                    <executions>
                        <execution>
                            <goals>
                                <goal>one-jar</goal>
                            </goals>
                        </execution>
                    </executions>
                    <configuration>
                        <onejarVersion>0.96</onejarVersion>
                        <mainClass>${main-class}</mainClass>
                        <attachToBuild>true</attachToBuild>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>

        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>aspectj-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.dstovall</groupId>
                <artifactId>onejar-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
            </plugin>
        </plugins>

    </build>

    <pluginRepositories>
        <pluginRepository>
            <id>OneJAR googlecode.com</id>
            <url>http://onejar-maven-plugin.googlecode.com/svn/mavenrepo</url>
        </pluginRepository>
    </pluginRepositories>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.aspectj</groupId>
                <artifactId>aspectjrt</artifactId>
                <version>${aspectj.version}</version>
                <scope>runtime</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
        </dependency>
    </dependencies>

    <organization>
        <name>Scrum-Master.de - Agile Project Management</name>
        <url>http://scrum-master.de</url>
    </organization>
</project>

Try to create a normal run configuration with the same start class as your AspectJ run configuration. 尝试使用与AspectJ运行配置相同的开始类创建一个正常运行配置。 When you create your runnable JAR, select the normal run configuration. 创建可运行的JAR时,请选择常规运行配置。 The resulting JAR will have AspectJ support. 最终的JAR将具有AspectJ支持。

You can find here the answer : https://www.eclipse.org/forums/index.php/t/204968/ 您可以在这里找到答案: https : //www.eclipse.org/forums/index.php/t/204968/

The runnable jar exporter only works with Java Application type launch configs , but that shouldn't be a problem. 可运行的jar导出器仅适用于Java Application类型的启动配置 ,但这不是问题。 Create a normal Java Application launch config with your main class and export the runnable jar based on that newly created launch config. 使用您的主类创建一个普通的Java Application启动配置,并基于该新创建的启动配置导出可运行的jar。 It should work. 它应该工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM